represents an external object bound to an ExternalSessionObject.
Details
- ExternalObject is a reference to external data that is returned by some ExternalEvaluate systems, such as "Python", "Ruby", "NodeJS".
- An ExternalObject[…] is specific to a particular ExternalSessionObject, which may not be persistent.
- ExternalObject[…]["name"] in most evaluators is identical to ExternalObject[…][ExternalOperation["GetAttribute", "name"]].
- ExternalObject[…][ExternalOperation[…]] is equivalent to ExternalEvaluate[session,ExternalOperation[…][ExternalObject[…]]], where session is implied by the ExternalObject[…].
- ExternalObject[…][op1,op2,…] is identical to ExternalEvaluate[session,RightComposition[op1,op2,…][ExternalObject[…]]].
- ExternalObject[…][{op1,op2,…}] is identical to ExternalEvaluate[session, {op1[ExternalObject[…]],op2[ExternalObject[…]],…}].
Examples
open allclose allScope (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:
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:
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:
"Call" (1)
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:
"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:
"SetAttribute" (1)
"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:
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:
Create an ExternalObject:
Use ExternalFunction to run methods on the object:
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:
Multiple operations can be concatenated using multiple arguments in ExternalObject:
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:
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:
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