WOLFRAM

ExternalObject
ExternalObject

Listing of External Evaluators »
Unsupported in Public CloudUpdated in 14.1[Experimental]

represents an external object bound to an ExternalSessionObject.

Details

Examples

open allclose all

Basic Examples  (1)Summary of the most common use cases

Create an external object in Python:

Out[1]=1

Scope  (7)Survey of the scope of standard use cases

The keys displayed for functions in Python differ between a built-in function and a user-defined function:

Out[1]=1

Define an object in Python and display it:

Out[2]=2

Delete the session:

External Operations  (6)

"Eval"  (2)

Start a Python session to work with strings:

Out[1]=1

Create an ExternalObject with a string that contains valid Python code:

Out[2]=2

Use the "Eval" operation to evaluate the string to Python code:

Out[3]=3

Close the session:

Start a Python session to work with strings:

Out[1]=1

Create an ExternalObject with a string that contains valid Python code with variables:

Out[2]=2

Use the "Eval" operation to evaluate the string to Python code, using an evaluation context:

Out[3]=3

Close the session:

"Call"  (1)

Start a Python session:

Out[1]=1

Define an ExternalObject that creates a function in Python:

Out[2]=2

Call the function by running the ExternalOperation "Call":

Out[3]=3

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

Out[4]=4

ExternalObject can also be used as an argument:

Out[5]=5
Out[6]=6

Arguments can also be passed directly in ExternalEvaluate by doing:

Out[7]=7

The result is equivalent to running the following Python code:

Out[8]=8

Close the session:

"GetAttribute"  (1)

Start a Python session to work with dates:

Return an ExternalObject for a datetime object:

Out[2]=2

Extract the year attribute by using "GetAttribute":

Out[3]=3

The same can be done using an ExternalObject subvalue:

Out[4]=4

For the Python evaluator, "GetAttribute" is the default operation, and ExternalOperation can be omitted:

Out[5]=5

The result is equivalent to running the following Python code:

Out[6]=6

Delete the session:

"SetAttribute"  (1)

Start a Python session to work with decimal numbers:

Get the context for the decimal module:

Out[2]=2

Set the precision to 6:

Check that the precision was set to 6:

Out[4]=4
Out[5]=5

The result is equivalent to running the following Python code:

Delete the session:

"Cast"  (1)

Create an ExternalObject that represents the current date:

Out[2]=2

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

Out[3]=3

The Cast operation can also run in ExternalObject subvalues:

Out[4]=4

The symbol Expression is a shortcut for the same:

Out[5]=5

Return the object as a string:

Out[6]=6

The symbol String is a shortcut for the same:

Out[7]=7

Return the object as an ExternalObject:

Out[8]=8

The symbol ExternalObject is a shortcut for the same:

Out[9]=9

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

Out[10]=10

Delete the session:

Properties & Relations  (6)Properties of the function, and connections to other functions

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

Out[1]=1

The same can be done with an ExternalFunction:

Out[2]=2

ExternalOperation can be used as an argument to ExternalObject:

Out[1]=1

The executed operation is identical to the following one:

Out[2]=2

Which can be executed with ExternalEvaluate:

Out[3]=3

The result is equivalent to running the following Python code:

Out[4]=4

Create an ExternalObject to work with the sys module:

Out[1]=1

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

Out[2]=2

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

Out[3]=3

Start a Python session:

Out[1]=1

Create an ExternalObject:

Out[2]=2

Use ExternalFunction to run methods on the object:

Out[3]=3

Close the session:

Start a Python session:

Out[1]=1

Create an ExternalObject for the datetime module:

Out[2]=2

Create an ExternalObject with the current date and another one with a timedelta:

Out[3]=3

Send back the objects using an ExternalFunction:

Out[4]=4

The same can be done with an ExternalEvaluate call:

Out[5]=5

Use "ReturnType" to return a String:

Out[6]=6

Close the session:

Multiple operations can be concatenated using multiple arguments in ExternalObject:

Out[1]=1
Out[2]=2

Which can be written in a more compact way:

Out[3]=3

Possible Issues  (1)Common pitfalls and unexpected behavior

Running successive operations using downvalues can lead to undefined behavior without checking the return value at every step:

Out[2]=2
Out[3]=3

This is happening because the first operation is returning an ExternalFunction (since date is a class that can be initialized) and you are passing an incomplete ExternalOperation to the date constructor:

Out[4]=4

In order to safely chain operations without checking the return type at every step, run all operations at once:

Out[5]=5

A Failure object is returned as soon as any operation fails, preventing others from running:

Out[6]=6

Delete the session:

Neat Examples  (1)Surprising or curious use cases

Start a Python session to play chess:

Out[1]=1

Create an ExternalObject to represent that state of the chess board:

Out[2]=2

Define a function to plot the board:

Use the function to plot the board:

Out[4]=4

Extract the current move number by extracting an attribute from the object:

Out[5]=5

Move a piece by running a function; it will change the state of the external object:

Out[6]=6

Plot the board again to display the new configuration:

Out[7]=7

You can now compute all legal moves that can be done with the current configuration:

Create a method caller for the "UCI" property by chaining external operations:

Out[9]=9

Compute the UCI string for one move:

Out[10]=10

You can now execute one random move:

Out[11]=11

You can use the plot function to show the board again:

Out[12]=12

Create a function to play random moves:

Play some random chess:

Out[14]=14

Show the current move counter:

Out[15]=15

Delete the session:

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

Text

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

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

CMS

Wolfram Language. 2018. "ExternalObject." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2024. https://reference.wolfram.com/language/ref/ExternalObject.html.

Wolfram Language. 2018. "ExternalObject." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2024. https://reference.wolfram.com/language/ref/ExternalObject.html.

APA

Wolfram Language. (2018). ExternalObject. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/ExternalObject.html

Wolfram Language. (2018). ExternalObject. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/ExternalObject.html

BibTeX

@misc{reference.wolfram_2025_externalobject, author="Wolfram Research", title="{ExternalObject}", year="2024", howpublished="\url{https://reference.wolfram.com/language/ref/ExternalObject.html}", note=[Accessed: 25-March-2025 ]}

@misc{reference.wolfram_2025_externalobject, author="Wolfram Research", title="{ExternalObject}", year="2024", howpublished="\url{https://reference.wolfram.com/language/ref/ExternalObject.html}", note=[Accessed: 25-March-2025 ]}

BibLaTeX

@online{reference.wolfram_2025_externalobject, organization={Wolfram Research}, title={ExternalObject}, year={2024}, url={https://reference.wolfram.com/language/ref/ExternalObject.html}, note=[Accessed: 25-March-2025 ]}

@online{reference.wolfram_2025_externalobject, organization={Wolfram Research}, title={ExternalObject}, year={2024}, url={https://reference.wolfram.com/language/ref/ExternalObject.html}, note=[Accessed: 25-March-2025 ]}