ExternalObject

Listing of External Evaluators »

ExternalObject[]

represents an external object bound to an ExternalSessionObject.

Details

Examples

open allclose all

Basic Examples  (1)

Create an external object in Python:

Scope  (7)

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

Define an object in Python and display it:

Delete the session:

External Operations  (6)

"Eval"  (2)

Start a Python session to work with strings:

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

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

Close the session:

Start a Python session to work with strings:

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

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

Close the session:

"Call"  (1)

Start a Python session:

Define an ExternalObject that creates a function in Python:

Call the function by running the ExternalOperation "Call":

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

ExternalObject can also be used as an argument:

Arguments can also be passed directly in ExternalEvaluate by doing:

The result is equivalent to running the following Python code:

Close the session:

"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 same can be done using an ExternalObject subvalue:

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

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  (6)

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 ExternalObject:

The executed operation is identical to the following 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:

Start a Python session:

Create an ExternalObject:

Use ExternalFunction to run methods on the object:

Close the session:

Start a Python session:

Create an ExternalObject for the datetime module:

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

Send back the objects using an ExternalFunction:

The same can be done with an ExternalEvaluate call:

Use "ReturnType" to return a String:

Close the session:

Multiple operations can be concatenated using multiple arguments in ExternalObject:

Which can be written in a more compact way:

Possible Issues  (1)

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

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:

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

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

Delete the session:

Neat Examples  (1)

Start a Python session to play chess:

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

Define a function to plot the board:

Use the function to plot the board:

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

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

Plot the board again to display the new configuration:

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:

Compute the UCI string for one move:

You can now execute one random move:

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

Create a function to play random moves:

Play some random chess:

Show the current move counter:

Delete the session:

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).

CMS

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

BibTeX

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

BibLaTeX

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