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

Examples

open allclose all

Basic Examples  (1)

Create an ExternalOperation to evaluate the code "2+2":

Run the operation using ExternalEvaluate:

Scope  (7)

ExternalOperation does not evaluate:

You can use downvalues to accumulate arguments:

Run the operation using ExternalEvaluate:

Define an operator form for the "GetAttribute" operation:

Start a Python session:

Create an ExternalObject:

Use the operator over the object to the specified attribute:

The same can be done by creating a new operation:

The operation needs to be evaluated by using ExternalEvaluate:

Close the session:

External Operations  (5)

"Eval"  (1)

Run an ExternalOperation that represents arbitrary code evaluation in Python:

Use the second argument to pass an evaluation context:

"Call"  (1)

Define an ExternalOperation that creates a function in Python:

Call the function by running the ExternalOperation "Call":

Run the operation using ExternalEvaluate:

Any argument of the "Call" operation can be an ExternalOperation:

Arguments can also be passed directly in ExternalEvaluate by doing:

The result is equivalent to running the following Python code:

"GetAttribute"  (1)

Start a Python session to work with dates:

Return an ExternalObject for a datetime object:

Extract the year attribute by using "GetAttribute":

The result is equivalent to running the following Python code:

Delete the session:

"SetAttribute"  (1)

Start a Python session to work with decimal numbers:

Get the context for the decimal module:

Set the precision to 6:

Check that the precision was set to 6:

The result is equivalent to running the following Python code:

Delete the session:

"Cast"  (1)

Create an ExternalObject that represents the current date:

Use "Expression" to return the object as a Wolfram Language expression:

The Cast operation can also run in ExternalObject subvalues:

The symbol Expression is a shortcut for the same:

Return the object as a string:

The symbol String is a shortcut for the same:

Return the object as an ExternalObject:

The symbol ExternalObject is a shortcut for the same:

The same can be achieved by using "ReturnType" in ExternalEvaluate:

Delete the session:

Properties & Relations  (5)

ExternalOperation can be used to extract the operation from an ExternalObject:

The same can be done with an ExternalFunction:

ExternalOperation can be used as an argument to ExternalFunction:

The same can be done by using ExternalEvaluate:

ExternalOperation can be used as an argument to ExternalObject:

The executed operation is identical to this one:

Which can be executed with ExternalEvaluate:

The result is equivalent to running the following Python code:

Create an ExternalObject to work with the sys module:

When a string is used, the "GetAttribute" operation is executed in most evaluators, including Python:

The result is equivalent to running an explicit "GetAttribute" operation:

Multiple operations can be concatenated using multiple arguments in ExternalObject:

Which can be written in a more compact way:

Wolfram Research (2024), ExternalOperation, Wolfram Language function, https://reference.wolfram.com/language/ref/ExternalOperation.html.

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_2024_externaloperation, author="Wolfram Research", title="{ExternalOperation}", year="2024", howpublished="\url{https://reference.wolfram.com/language/ref/ExternalOperation.html}", note=[Accessed: 17-November-2024 ]}

BibLaTeX

@online{reference.wolfram_2024_externaloperation, organization={Wolfram Research}, title={ExternalOperation}, year={2024}, url={https://reference.wolfram.com/language/ref/ExternalOperation.html}, note=[Accessed: 17-November-2024 ]}