-
See Also
- StartExternalSession
- ExternalEvaluate
- ExternalObject
- ExternalFunction
- ExternalSessionObject
-
- External Evaluation Systems
- Python
- NodeJS
- Ruby
- Related Guides
- Workflows
-
-
See Also
- StartExternalSession
- ExternalEvaluate
- ExternalObject
- ExternalFunction
- ExternalSessionObject
-
- External Evaluation Systems
- Python
- NodeJS
- Ruby
- Related Guides
- Workflows
-
See Also
ExternalOperation["Eval","code"]
represents an external evaluation of "code".
ExternalOperation["Eval","code",assoc]
represents an external evaluation of "code" with parameters given by assoc.
ExternalOperation["Call",func,arg1,arg2,…]
calls the function func with the given arguments arg1, arg2, ….
ExternalOperation["GetAttribute",obj,"attr"]
gets the attribute "attr" of obj.
ExternalOperation["SetAttribute",obj,"attr",val]
sets the attribute "attr" of obj to the given value val.
ExternalOperation["Cast",obj,"type"]
casts obj to the given "type".
ExternalOperation["op", arg1,arg2,…]
represents the external operation named "op" using arguments arg1, arg2, ….
ExternalOperation
Listing of External Evaluators »ExternalOperation["Eval","code"]
represents an external evaluation of "code".
ExternalOperation["Eval","code",assoc]
represents an external evaluation of "code" with parameters given by assoc.
ExternalOperation["Call",func,arg1,arg2,…]
calls the function func with the given arguments arg1, arg2, ….
ExternalOperation["GetAttribute",obj,"attr"]
gets the attribute "attr" of obj.
ExternalOperation["SetAttribute",obj,"attr",val]
sets the attribute "attr" of obj to the given value val.
ExternalOperation["Cast",obj,"type"]
casts obj to the given "type".
ExternalOperation["op", arg1,arg2,…]
represents the external operation named "op" using arguments arg1, arg2, ….
Details
- ExternalOperation represents a remote procedure call to be executed in an ExternalEvaluate system, such as "Python", "Ruby", "NodeJS".
- Available operations "op" in ExternalOperation["op",…] vary from system to system.
- ExternalOperation[…] can be evaluated with ExternalEvaluate["system",ExternalOperation[…]].
- ExternalOperation["op",params…][args…] evaluates to ExternalOperation["op",args…,params…].
- ExternalObject[…][ExternalOperation[…]] is equivalent to ExternalEvaluate["system",ExternalOperation[…][ExternalObject[…]]].
Examples
open all close allBasic Examples (1)
Create an ExternalOperation to evaluate the code "2+2":
op = ExternalOperation["Eval", "2+2"]Run the operation using ExternalEvaluate:
ExternalEvaluate["Python", op]Scope (7)
ExternalOperation does not evaluate:
call = ExternalOperation["Call"]You can use downvalues to accumulate arguments:
op = call[ExternalOperation["Eval", "max"], 1, 2]Run the operation using ExternalEvaluate:
ExternalEvaluate["Python", op]Define an operator form for the "GetAttribute" operation:
yeargetter = ExternalOperation["GetAttribute", "year"]session = StartExternalSession["Python"]Create an ExternalObject:
date = ExternalEvaluate[session, <|"Command" -> ExternalOperation["Import", "datetime", "date"], "Arguments" -> {2012, 10, 10}, "ReturnType" -> "ExternalObject"|>]Use the operator over the object to the specified attribute:
date[yeargetter]The same can be done by creating a new operation:
year = yeargetter[date]The operation needs to be evaluated by using ExternalEvaluate:
ExternalEvaluate[session, year]DeleteObject[session]External Operations (5)
"Eval" (1)
Run an ExternalOperation that represents arbitrary code evaluation in Python:
ExternalEvaluate["Python", ExternalOperation["Eval", "2+2"]]Use the second argument to pass an evaluation context:
ExternalEvaluate["Python", ExternalOperation["Eval", "a + b", <|"a" -> 2, "b" -> 3|>]]"Call" (1)
Define an ExternalOperation that creates a function in Python:
op = ExternalOperation["Eval", "max"]Call the function by running the ExternalOperation "Call":
call = ExternalOperation["Call", op, 1, 2]Run the operation using ExternalEvaluate:
ExternalEvaluate["Python", call]Any argument of the "Call" operation can be an ExternalOperation:
ExternalEvaluate["Python", ExternalOperation["Call", op, 1, ExternalOperation["Eval", "2+2"]]]Arguments can also be passed directly in ExternalEvaluate by doing:
ExternalEvaluate["Python", op -> {1, 2}]The result is equivalent to running the following Python code:
ExternalEvaluate["Python", "max(1, 2)"]"GetAttribute" (1)
Start a Python session to work with dates:
session = StartExternalSession[{"Python", "SessionProlog" -> "import datetime"}];Return an ExternalObject for a datetime object:
now = ExternalEvaluate[session, <|"Command" -> "datetime.datetime.now()", "ReturnType" -> "ExternalObject"|>]Extract the year attribute by using "GetAttribute":
ExternalEvaluate[session, ExternalOperation["GetAttribute", now, "year"]]The result is equivalent to running the following Python code:
ExternalEvaluate[session, "datetime.datetime.now().year"]DeleteObject[session]"SetAttribute" (1)
Start a Python session to work with decimal numbers:
session = StartExternalSession[{"Python", "SessionProlog" -> "from decimal import getcontext, Decimal"}];Get the context for the decimal module:
context = ExternalEvaluate[session, "getcontext()"]ExternalEvaluate[session, ExternalOperation["SetAttribute", context, "prec", 6]]Check that the precision was set to 6:
ExternalEvaluate[session, ExternalOperation["GetAttribute", context, "prec"]]ExternalEvaluate[session, "Decimal('0') + Decimal('0.123456789')"]The result is equivalent to running the following Python code:
ExternalEvaluate[session, "getcontext().prec = 6"]DeleteObject[session]"Cast" (1)
Create an ExternalObject that represents the current date:
session = StartExternalSession["Python"];now = ExternalEvaluate[session, <|"Command" -> "import datetime; datetime.datetime.now()", "ReturnType" -> "ExternalObject"|>]Use "Expression" to return the object as a Wolfram Language expression:
ExternalEvaluate[session, ExternalOperation["Cast", now, "Expression"]]The Cast operation can also run in ExternalObject subvalues:
now[ExternalOperation["Cast", "Expression"]]The symbol Expression is a shortcut for the same:
now[Expression]Return the object as a string:
now[ExternalOperation["Cast", "String"]]The symbol String is a shortcut for the same:
now[String]Return the object as an ExternalObject:
now[ExternalOperation["Cast", "ExternalObject"]]The symbol ExternalObject is a shortcut for the same:
now[ExternalObject]The same can be achieved by using "ReturnType" in ExternalEvaluate:
ExternalEvaluate[session, <|"Command" -> now, "ReturnType" -> "String"|>]DeleteObject[session]Properties & Relations (5)
ExternalOperation can be used to extract the operation from an ExternalObject:
ExternalOperation[ExternalObject["Python", "2+2"]]The same can be done with an ExternalFunction:
ExternalOperation[ExternalFunction["Python", "str"]]ExternalOperation can be used as an argument to ExternalFunction:
ExternalFunction["Python", "len"][ExternalOperation["Eval", "[1, 2, 3]"]]The same can be done by using ExternalEvaluate:
ExternalEvaluate["Python", "len" -> ExternalOperation["Eval", "[1, 2, 3]"]]ExternalOperation can be used as an argument to ExternalObject:
ExternalObject["Python", "len"][ExternalOperation["Call", {1, 2, 3}]]The executed operation is identical to this one:
op = ExternalOperation["Call", {1, 2, 3}][ExternalObject["Python", "len"]]Which can be executed with ExternalEvaluate:
ExternalEvaluate["Python", op]The result is equivalent to running the following Python code:
ExternalEvaluate["Python", "len((1, 2, 3))"]Create an ExternalObject to work with the sys module:
sys = ExternalObject["Python", ExternalOperation["Import", "sys"]]When a string is used, the "GetAttribute" operation is executed in most evaluators, including Python:
sys["maxsize"]The result is equivalent to running an explicit "GetAttribute" operation:
sys[ExternalOperation["GetAttribute", "maxsize"]]Multiple operations can be concatenated using multiple arguments in ExternalObject:
uuid = ExternalObject["Python", ExternalOperation["Import", "uuid"]]uuid[ExternalOperation["GetAttribute", "uuid4"], ExternalOperation["Call"], ExternalOperation["Cast", "String"]]Which can be written in a more compact way:
uuid["uuid4" -> {}, String]Related Guides
History
Text
Wolfram Research (2024), ExternalOperation, Wolfram Language function, https://reference.wolfram.com/language/ref/ExternalOperation.html.
CMS
Wolfram Language. 2024. "ExternalOperation." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/ExternalOperation.html.
APA
Wolfram Language. (2024). ExternalOperation. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/ExternalOperation.html
BibTeX
@misc{reference.wolfram_2026_externaloperation, author="Wolfram Research", title="{ExternalOperation}", year="2024", howpublished="\url{https://reference.wolfram.com/language/ref/ExternalOperation.html}", note=[Accessed: 13-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_externaloperation, organization={Wolfram Research}, title={ExternalOperation}, year={2024}, url={https://reference.wolfram.com/language/ref/ExternalOperation.html}, note=[Accessed: 13-June-2026]}