Wolfram Function Repository
Instant-use add-on functions for the Wolfram Language
Function Repository Resource:
Create a Wolfram Language representation of a Python object
ResourceFunction["PythonObject"][<|…|>] represents a Python object defined in an ExternalSessionObject. | |
ResourceFunction["PythonObject"][session,"cmd"] evaluates cmd in the specified running ExternalSessionObject and creates a reference to the resulting external object. | |
ResourceFunction["PythonObject"]["cmd"] starts a new external session and evaluates cmd. | |
ResourceFunction["PythonObject"][…][prop] gives the specified property of the ResourceFunction["PythonObject"][…]. | |
ResourceFunction["PythonObject"][…][func[args,opts]] calls the function func defined in the Python object with the specified arguments and options. | |
ResourceFunction["PythonObject"][…][All][args,opts]] invokes the callable Python object with the specified arguments and options. |
| Initialization | Automatic | Python comands to execute prior to creating the object |
| "Configuration" | Automatic | custom package-level setup |
| "LocalCache" | Automatic | whether cached values should be persistent |
| "cmd1;cmd2;…" | execute the specified commands |
| {"cmd1","cmd2",…,Automatic} | execute cmdi prior to the automatic initialization |
| None | no initialization |
| ResourceFunction["f"] | resource function |
| "Custom" | interactively defined |
| Automatic | automatic |
| None | no configuration |
| "Aliases" | aliases of a function or class |
| "CacheLocation" | persistent local cache |
| "Configuration" | configuration |
| "FullInformation" | recursive information for modules |
| "Information" | Python-side information |
| "PythonReference" | a reference to the Python-side object |
| "RawInformation" | Python-side help |
| "ReferenceValid" | whether the reference corresponds to an active Python session |
| "Session" | ExternalSessionObject |
| "IsCallable" | callable object |
| "IsModule" | module |
| "IsClass" | class |
| "IsFunction" | function |
| "IsBuiltin" | built-in function |
| "Arguments" | arguments |
| "Options" | options |
| "VarArgs" | whether the function accepts a variable number of positional arguments |
| "VarKeywords" | whether the function accepts arbitrary named arguments |
| "KeywordOnlyArguments" | arguments that must be specified by name |
| "KeywordOnlyDefaults" | default values of named arguments |
| "Functions" | functions |
| "Classes" | classes |
| "Modules" | modules |
| "Part" | part extraction |
| "Assign" | part or variable assignment |
| "Evaluate" | Python code evaluation |
| "var" | ResourceFunction["PythonObject"] variable |
| "var"["var1"]["var2"]… | vari components of of the variable var |
| "Part"[…] | part specification for list-like objects |
| BitAnd | and_ |
| BitOr | or_ |
| BitXor | xor |
| BitNot | invert |
| BitShiftLeft | lshift |
| BitShiftRight | rshift |
Create a Python object for a package:
| In[1]:= |
| Out[1]= | ![]() |
Explore its contents:
| In[2]:= |
| Out[2]= |
Create an instance of a class in the package:
| In[3]:= |
| Out[3]= | ![]() |
Examine its contents:
| In[4]:= |
| Out[4]= |
Get a the value of an instance variable:
| In[5]:= |
| Out[5]= |
Find the signature of a method:
| In[6]:= |
| Out[6]= |
Call the method:
| In[7]:= |
| Out[7]= |
Uninstall the package and close the session to clean up:
| In[8]:= |
| Out[8]= |
| In[9]:= |
Define a simple class in Python:
| In[10]:= |
| Out[10]= |
| In[11]:= | ![]() |
| Out[11]= |
Create a Python object referring to the class:
| In[12]:= |
| Out[12]= | ![]() |
Get the value of a class variable from Python:
| In[13]:= |
| Out[13]= |
Create an instance of the class:
| In[14]:= |
| Out[14]= | ![]() |
Check the instance variable:
| In[15]:= |
| Out[15]= |
Set the value of the variable and check the new value:
| In[16]:= |
| Out[16]= |
| In[17]:= |
| Out[17]= |
Call a method function of the instance:
| In[18]:= |
| Out[18]= |
Check the new instance variables:
| In[19]:= |
| Out[19]= |
Close the session:
| In[20]:= |
A Python command specified as a String:
| In[21]:= |
| Out[21]= | ![]() |
| In[22]:= |
The command specified by an Association:
| In[23]:= |
| Out[23]= | ![]() |
Multiple commands:
| In[24]:= |
| Out[24]= | ![]() |
| In[25]:= |
Components of a Python list:
| In[26]:= |
| Out[26]= | ![]() |
| In[27]:= |
Python dictionaries containing objects:
| In[28]:= |
| In[29]:= |
| Out[29]= | ![]() |
| In[30]:= |
Create a PythonObject[…] only for those objects that do not have a Wolfram Language representation:
| In[31]:= |
| In[32]:= |
| In[33]:= |
| Out[33]= | ![]() |
Create Python objects for all objects, even the simplest:
| In[34]:= |
| Out[34]= | ![]() |
| In[35]:= |
Get a list of items in a Python tuple:
| In[36]:= |
| In[37]:= |
| Out[37]= |
| In[38]:= |
| Out[38]= | ![]() |
Get the tuple as a PythonObject:
| In[39]:= |
| Out[39]= | ![]() |
Get the tuple components:
| In[40]:= |
| Out[40]= | ![]() |
| In[41]:= |
Define an instance object in Python and access its Python-side property:
| In[42]:= |
| In[43]:= | ![]() |
| Out[43]= |
| In[44]:= |
| Out[44]= | ![]() |
| In[45]:= |
| Out[45]= |
Assign a new value to the property:
| In[46]:= |
| Out[46]= |
| In[47]:= |
| Out[47]= |
Call a method:
| In[48]:= |
| Out[48]= |
Alternatively, define a Python object referring to the class and construct an instance on the Wolfram Language side:
| In[49]:= |
| Out[49]= | ![]() |
| In[50]:= |
| Out[50]= | ![]() |
The new value of the instance variable:
| In[51]:= |
| Out[51]= |
Call a member function:
| In[52]:= |
| Out[52]= |
| In[53]:= |
Get a part of a list-like object without bringing the object from Python:
| In[54]:= |
| Out[54]= | ![]() |
| In[55]:= |
| Out[55]= |
| In[56]:= |
| Out[56]= |
Get spans of elements:
| In[57]:= |
| Out[57]= |
| In[58]:= |
| Out[58]= |
| In[59]:= |
| Out[59]= |
| In[60]:= |
| Out[60]= |
Get all parts:
| In[61]:= |
| Out[61]= |
| In[62]:= |
Get a part of a multidimensional Python array:
| In[63]:= |
| Out[63]= |
| In[64]:= |
| Out[64]= | ![]() |
| In[65]:= |
| Out[65]= |
Get a span of elements:
| In[66]:= |
| Out[66]= |
| In[67]:= |
Define a Python list object:
| In[68]:= |
| Out[68]= | ![]() |
| In[69]:= |
| Out[69]= |
Assign a new value to a part of the list:
| In[70]:= |
| Out[70]= |
| In[71]:= |
| Out[71]= |
Assign a new value to a span of elements:
| In[72]:= |
| Out[72]= |
| In[73]:= |
| Out[73]= |
| In[74]:= |
Define a Python class whose variable is an instance of another class:
| In[75]:= |
| Out[75]= |
| In[76]:= | ![]() |
| Out[76]= |
| In[77]:= |
| Out[77]= | ![]() |
The value of the variable c, "a.b.c" in the Python notation:
| In[78]:= |
| Out[78]= |
Assign the value to c:
| In[79]:= |
| Out[79]= |
Check the value on the Python side:
| In[80]:= |
| Out[80]= | ![]() |
| In[81]:= |
| Out[81]= |
Alternatively:
| In[82]:= |
| Out[82]= |
| In[83]:= |
Define a Python object:
| In[84]:= |
| Out[84]= | ![]() |
Evaluate Python code referencing the object:
| In[85]:= |
Equivalently:
| In[86]:= |
| In[87]:= |
Supply arguments to be converted to Python expressions:
| In[88]:= |
String arguments are passed verbatim, without extra quotation marks:
| In[89]:= |
Escape quotation marks to pass an expression as a string:
| In[90]:= |
| Out[90]= |
Or use single quotes inside the string:
| In[91]:= |
| Out[91]= |
| In[92]:= |
Interactively defined object:
| In[93]:= |
| Out[93]= | ![]() |
| In[94]:= |
A module in Python's standard library:
| In[95]:= |
| Out[95]= | ![]() |
| In[96]:= |
A class in the standard library:
| In[97]:= |
| Out[97]= | ![]() |
| In[98]:= |
An instance of a class in the standard library:
| In[99]:= |
| Out[99]= | ![]() |
| In[100]:= |
A function in the standard library:
| In[101]:= |
| Out[101]= | ![]() |
| In[102]:= |
A built-in function:
| In[103]:= |
| Out[103]= | ![]() |
| In[104]:= |
An installable package, with its classes and functions:
| In[105]:= |
| In[106]:= |
| Out[106]= | ![]() |
| In[107]:= |
| Out[107]= | ![]() |
| In[108]:= |
| Out[108]= | ![]() |
| In[109]:= |
Python's positional arguments are supported. Define a function that takes positional arguments:
| In[110]:= |
| Out[110]= |
| In[111]:= | ![]() |
| Out[111]= |
The Python object representing the function:
| In[112]:= |
| Out[112]= | ![]() |
Call the function with the mandatory positional argument:
| In[113]:= |
| Out[113]= |
Supply a value of the optional argument:
| In[114]:= |
| Out[114]= |
| In[115]:= |
Positional arguments can have variable lengths. Define a function that takes an arbitrary number of positional arguments in a Python session:
| In[116]:= |
| Out[116]= |
| In[117]:= | ![]() |
| Out[117]= |
The Python object representing the function:
| In[118]:= |
| Out[118]= | ![]() |
The parameter "VarArgs" is True for information on a function that takes variable-length positional arguments:
| In[119]:= |
| Out[119]= |
Call the function with one or more arguments:
| In[120]:= |
| Out[120]= |
| In[121]:= |
| Out[121]= |
| In[122]:= |
Named arguments are supported. Define a Python function that takes an arbitrary number of positional arguments as well as named arguments, with or without default values:
| In[123]:= |
| Out[123]= |
| In[124]:= | ![]() |
The Python object representing the function:
| In[125]:= |
| Out[125]= | ![]() |
Call the function with one positional and one named argument:
| In[126]:= |
| Out[126]= |
Call the function with positional and named arguments:
| In[127]:= |
| Out[127]= |
| In[128]:= |
Arbitrary names can be used. Define a Python function that takes arbitrary named arguments:
| In[129]:= |
| Out[129]= |
| In[130]:= | ![]() |
The Python object representing the function:
| In[131]:= |
| Out[131]= | ![]() |
Call the function supplying a few arbitrary named arguments:
| In[132]:= |
| Out[132]= |
| In[133]:= |
Perform basic arithmetic operations on objects on the Python side:
| In[134]:= |
| In[135]:= |
| Out[135]= | ![]() |
| In[136]:= |
| Out[136]= | ![]() |
| In[137]:= |
| Out[137]= | ![]() |
| In[138]:= |
| Out[138]= |
The absolute value:
| In[139]:= |
| Out[139]= |
The modulo operation:
| In[140]:= |
| Out[140]= |
| In[141]:= |
Compare objects in Python, without bringing them to Wolfram Language:
| In[142]:= |
| In[143]:= |
| Out[143]= | ![]() |
| In[144]:= |
| Out[144]= | ![]() |
| In[145]:= |
| Out[145]= |
| In[146]:= |
Bitwise operations in Python:
| In[147]:= |
| In[148]:= |
| Out[148]= | ![]() |
| In[149]:= |
| Out[149]= | ![]() |
| In[150]:= |
| In[151]:= |
| Out[151]= |
Compare with the same operations in Wolfram Language:
| In[152]:= |
| Out[152]= |
| In[153]:= |
| Out[153]= |
Bit inverse:
| In[154]:= |
| Out[154]= |
| In[155]:= |
| Out[155]= |
| In[156]:= |
Logical operations:
| In[157]:= |
| In[158]:= |
| Out[158]= | ![]() |
| In[159]:= |
| Out[159]= | ![]() |
| In[160]:= |
| Out[160]= |
| In[161]:= |
| Out[161]= |
| In[162]:= |
| Out[162]= |
Logical operations done via bitwise operations:
| In[163]:= |
| Out[163]= | ![]() |
| In[164]:= |
| Out[164]= |
| In[165]:= |
| Out[165]= |
| In[166]:= |
Test membership:
| In[167]:= |
| Out[167]= | ![]() |
| In[168]:= |
| Out[168]= | ![]() |
| In[169]:= |
| Out[169]= |
| In[170]:= |
A class:
| In[171]:= |
| In[172]:= | ![]() |
| Out[172]= |
| In[173]:= |
| Out[173]= | ![]() |
Create an instance of the class:
| In[174]:= |
| Out[174]= | ![]() |
| In[175]:= |
A callable instance of the class:
| In[176]:= |
| In[177]:= | ![]() |
| Out[177]= |
| In[178]:= |
| Out[178]= | ![]() |
Call the instance:
| In[179]:= |
| Out[179]= |
| In[180]:= |
A function:
| In[181]:= |
| In[182]:= | ![]() |
| Out[182]= |
| In[183]:= |
| Out[183]= | ![]() |
Call the function:
| In[184]:= |
| Out[184]= |
| In[185]:= |
Call a function defined in a module via the parent module object:
| In[186]:= |
| In[187]:= |
| Out[187]= | ![]() |
| In[188]:= |
| In[189]:= |
| Out[189]= | ![]() |
| In[190]:= |
| Out[190]= |
Alternatively, define a callable function object and call it directly:
| In[191]:= |
| Out[191]= | ![]() |
| In[192]:= |
| Out[192]= | ![]() |
| In[193]:= |
| Out[193]= |
| In[194]:= |
Define a Python class:
| In[195]:= |
| In[196]:= | ![]() |
| Out[196]= |
Create a Python object referring to the class:
| In[197]:= |
| Out[197]= | ![]() |
In PythonObject, optional arguments of a callable Python object become standard Wolfram Language options:
| In[198]:= |
| Out[198]= |
Create an instance and check its variables:
| In[199]:= |
| Out[199]= | ![]() |
| In[200]:= |
| Out[200]= |
For comparison, specify the optional argument in the Python code and check the new instance variables:
| In[201]:= |
| Out[201]= |
| In[202]:= |
Define a Python class in which the constructor takes a function as an argument:
| In[203]:= |
| In[204]:= | ![]() |
| Out[204]= |
Create a Python object referring to the class:
| In[205]:= |
| Out[205]= | ![]() |
To create an instance of the class, define a function on the Python side:
| In[206]:= | ![]() |
| Out[206]= |
Supply the ExternalFunction object to the class constructor::
| In[207]:= |
| Out[207]= | ![]() |
Invoke a method of the class:
| In[208]:= |
Alternatively, initialize an instance with a PythonObject:
| In[209]:= |
| Out[209]= | ![]() |
| In[210]:= |
| Out[210]= | ![]() |
Invoke the method:
| In[211]:= |
| In[212]:= |
Define a Python function:
| In[213]:= |
| Out[213]= |
| In[214]:= |
| Out[214]= | ![]() |
Pass a NumericArray to the function:
| In[215]:= |
| Out[215]= | ![]() |
Define a Python function which expects a specific type of a Python object, for instance, the enum Color, as an option:
| In[216]:= |
| In[217]:= | ![]() |
| In[218]:= |
| Out[218]= | ![]() |
A Python object for the enum:
| In[219]:= |
| Out[219]= | ![]() |
Supply an enum value to the function:
| In[220]:= |
| Out[220]= |
| In[221]:= |
Define a class in Python:
| In[222]:= |
| In[223]:= | ![]() |
Create a Python object referring to the class:
| In[224]:= |
| Out[224]= | ![]() |
Call a method function and get the instance variable using strings as names:
| In[225]:= |
| Out[207]= |
Alternatively, use symbols as names:
| In[226]:= |
| Out[209]= |
| In[227]:= |
Define a Python package with a variable and a function that both return lists:
| In[228]:= |
| In[229]:= | ![]() |
| In[230]:= |
| Out[230]= | ![]() |
Variable as a list:
| In[231]:= |
| Out[231]= |
Use the policy argument to keep the list on the Python side:
| In[232]:= |
| Out[232]= | ![]() |
Function result as a list:
| In[233]:= |
| Out[233]= |
Create a Python-side object using the policy argument:
| In[234]:= |
| Out[234]= | ![]() |
Callable:
| In[235]:= |
| Out[235]= | ![]() |
| In[236]:= |
| Out[236]= |
Invoke the callable with the policy argument:
| In[237]:= |
| Out[237]= | ![]() |
| In[238]:= |
Create a Python object for a list:
| In[239]:= |
| Out[239]= | ![]() |
Bring the result to the Wolfram Language:
| In[240]:= |
| Out[240]= |
| In[241]:= |
Deploy a temporary package that defines a class with a class method:
| In[242]:= |
| In[243]:= | ![]() |
| In[244]:= |
| Out[244]= | ![]() |
Create a Python object referring to the class:
| In[245]:= |
| Out[245]= | ![]() |
Create a Python object referring to the class method:
| In[246]:= |
| Out[246]= | ![]() |
Call the method and notice that the class object is passed to the class method as the first argument:
| In[247]:= |
| Out[247]= | ![]() |
| In[248]:= |
Create a decorated function in a Python session:
| In[249]:= |
| Out[249]= |
| In[250]:= | ![]() |
| Out[250]= |
The Python object representing the function:
| In[251]:= |
| Out[251]= | ![]() |
The signature of the decorated function is inferred correctly, despite it being masked by the decoration:
| In[252]:= |
| Out[252]= |
Call the function with the mandatory and optional arguments:
| In[253]:= |
| Out[253]= |
| In[254]:= |
| Out[254]= |
| In[255]:= |
Without initialization, PythonObject fails for this command because the required package is not available in the current session:
| In[256]:= |
| In[257]:= |
| Out[257]= |
In fact, the package is not even installed:
| In[258]:= |
| Out[258]= |
With the default setting Initialization→Automatic, the package is automatically installed and imported:
| In[259]:= |
| Out[259]= | ![]() |
The package is now installed:
| In[260]:= |
| Out[260]= |
Uninstall to clean up:
| In[261]:= |
| Out[261]= |
| In[262]:= |
Specify a custom initialization:
| In[263]:= |
| Out[263]= | ![]() |
Use the new object:
| In[264]:= |
| Out[264]= |
Uninstall the package and close the session to clean up:
| In[265]:= |
| Out[265]= |
| In[266]:= |
Without configuration, the raw Python's uuid4() function returns an instance of the UUID class:
| In[267]:= |
| Out[267]= | ![]() |
| In[268]:= |
| Out[268]= | ![]() |
| In[269]:= |
| Out[269]= |
| In[270]:= |
Specify a configuration defined in a resource function:
| In[271]:= |
| Out[271]= |
The custom configuration icon alerts you to the fact that the object has a non-default behavior. In this case, the return value of the Python object changes to a string:
| In[272]:= |
| Out[272]= |
| In[273]:= |
| Out[273]= |
| In[274]:= |
By default, PythonObject does not cache information for smaller packages:
| In[275]:= |
| Out[275]= | ![]() |
| In[276]:= |
| Out[276]= |
Force caching the information:
| In[277]:= |
| Out[277]= | ![]() |
| In[278]:= |
| Out[278]= | ![]() |
Delete the cache:
| In[279]:= |
| In[280]:= |
| Out[280]= |
| In[281]:= |
Create a PythonObject to efficiently reuse data on the Python side, even when you do not have access to the Python code:
| In[282]:= |
Execute some code that defines a big object and keep a reference to that object:
| In[283]:= |
| In[284]:= |
| Out[284]= | ![]() |
Use the object:
| In[285]:= |
| Out[285]= |
| In[286]:= |
Make a function defined in an external module available in your Wolfram Language session:
| In[287]:= |
| Out[287]= | ![]() |
The function signature:
| In[288]:= |
| Out[288]= |
Call the function:
| In[289]:= |
| Out[289]= | ![]() |
Information about the returned object:
| In[290]:= |
| Out[290]= | ![]() |
The value of one of the available variables:
| In[291]:= |
| Out[291]= |
Another variable refers to a more complex object:
| In[292]:= |
| Out[292]= | ![]() |
| In[293]:= |
| Out[293]= |
Get the signature of one of the methods of the new object:
| In[294]:= |
| Out[294]= |
Call the method several times:
| In[295]:= |
| Out[295]= |
| In[296]:= |
Create a Python object for an external package, for instance, Topoly:
| In[297]:= |
| Out[297]= | ![]() |
Explore its contents:
| In[298]:= |
| Out[298]= | ![]() |
Find the signature of a function from the package:
| In[299]:= |
| Out[299]= | ![]() |
Prepare and plot a list of coordinates that represent a 51knot:
| In[300]:= |
| In[301]:= |
| Out[301]= | ![]() |
Call the Topoly function and display its result; in this case, a plot of the fingerprint matrix for the coordinates of the knot:
| In[302]:= |
| In[303]:= | ![]() |
| In[304]:= |
| Out[304]= | ![]() |
| In[305]:= |
Create a Python object for the igraph package:
| In[306]:= |
| Out[306]= | ![]() |
Create an igraph "Graph" object:
| In[307]:= |
| Out[307]= | ![]() |
Read a GraphML file from ExampleData:
| In[308]:= | ![]() |
| In[309]:= |
| Out[309]= | ![]() |
An igraph object for the vertex list:
| In[310]:= |
| Out[310]= | ![]() |
Find the shortest path between vertices number 1 and 7:
| In[311]:= |
| Out[311]= |
To compare with the Wolfram Language result, import the graph:
| In[312]:= |
| Out[312]= | ![]() |
Find the shortest path between the vertices:
| In[313]:= |
| Out[313]= |
Compare the paths:
| In[314]:= |
| Out[314]= |
| In[315]:= |
| Out[315]= |
| In[316]:= |
Some common properties:
| In[317]:= |
| Out[317]= | ![]() |
| In[318]:= |
| Out[318]= | ![]() |
Curated properties:
| In[319]:= |
| Out[319]= |
| In[320]:= |
Contents of a package:
| In[321]:= |
| Out[321]= | ![]() |
| In[322]:= |
| Out[322]= | ![]() |
Recurse into submodules:
| In[323]:= |
| Out[323]= | ![]() |
All modules in the package:
| In[324]:= |
| Out[324]= | ![]() |
Contents of a module:
| In[325]:= |
| Out[325]= | ![]() |
| In[326]:= |
| Out[326]= | ![]() |
| In[327]:= |
Get a list of all functions in the SciPy package:
| In[328]:= |
| Out[328]= | ![]() |
| In[329]:= |
| Out[329]= |
Functions of the same name that appear in multiple contexts:
| In[330]:= |
| Out[330]= | ![]() |
Access the matrix exponentials for dense and sparse matrices:
| In[331]:= |
| Out[331]= | ![]() |
| In[332]:= |
| Out[332]= |
| In[333]:= |
| Out[333]= | ![]() |
| In[334]:= |
| Out[334]= |
The function with a shorter context path can be also accessed by its shorter name:
| In[335]:= |
| Out[335]= | ![]() |
| In[336]:= |
Information on a class, including the class variable and the methods:
| In[337]:= |
| In[338]:= | ![]() |
| Out[338]= |
| In[339]:= |
| Out[339]= | ![]() |
| In[340]:= |
| Out[340]= |
Information on a class instance, including the class and instance variables and the methods:
| In[341]:= |
| Out[341]= | ![]() |
| In[342]:= |
| Out[342]= |
| In[343]:= |
Obtain the signature of a callable object:
| In[344]:= |
| Out[344]= | ![]() |
| In[345]:= |
| Out[345]= | ![]() |
| In[346]:= |
Access Python's "built-ins", mathematical functions defined by the C standard:
| In[347]:= |
| Out[347]= | ![]() |
| In[348]:= |
| Out[348]= | ![]() |
| In[349]:= |
| Out[349]= |
| In[350]:= |
| Out[350]= |
| In[351]:= |
Even if not listed in "Properties", native Python attributes are also accessible:
| In[352]:= |
| In[353]:= |
| Out[353]= | ![]() |
| In[354]:= |
| Out[354]= | ![]() |
| In[355]:= |
Similarly, you can use double underscore methods ("dunders"):
| In[356]:= |
| Out[356]= | ![]() |
| In[357]:= |
| Out[357]= |
| In[358]:= |
| Out[358]= |
| In[359]:= |
Get the Python-side help:
| In[360]:= |
| In[361]:= |
| In[362]:= |
Use the "PythonReference" property to access the object on the Python side with ExternalEvaluate:
| In[363]:= |
| Out[363]= | ![]() |
| In[364]:= |
| Out[364]= | ![]() |
The explicit "PythonReference" specification can be omitted since it is the default property:
| In[365]:= |
| Out[365]= |
| In[366]:= |
| Out[366]= |
| In[367]:= |
"PythonReference" becomes stale if you delete the Python object:
| In[368]:= |
| Out[364]= |
| In[369]:= |
| Out[366]= |
"PythonReference" also stales if you close the session in which it is defined:
| In[370]:= | ![]() |
| Out[343]= |
PythonObject is similar to ExternalObject returned by ExternalEvaluate:
| In[371]:= |
| In[372]:= |
| Out[372]= | ![]() |
| In[373]:= |
| Out[373]= |
PythonObject is also similar to an ExternalFunction object that can be defined to call a Python function:
| In[374]:= |
| Out[374]= |
| In[375]:= |
| Out[375]= | ![]() |
| In[376]:= |
| Out[376]= |
Call the two functions:
| In[377]:= |
| In[378]:= |
| Out[378]= | ![]() |
The major difference between the produced objects is that the contents of the latter are accessible directly in Wolfram Language without any Python programming:
| In[379]:= |
| Out[379]= | ![]() |
| In[380]:= |
| Out[380]= | ![]() |
| In[381]:= |
For callable objects, PythonObject[…][All] is used only for calling the object and otherwise returns unevaluated:
| In[382]:= |
| Out[382]= | ![]() |
| In[383]:= |
| Out[383]= | ![]() |
| In[384]:= |
| Out[384]= | ![]() |
| In[385]:= |
Create and deploy a Python package with one unique function name, two identically named functions at different depth, and another two identically named functions at the same depth in two modules:
| In[386]:= |
| In[387]:= | ![]() |
| Out[387]= | ![]() |
Call the unique function by its name:
| In[388]:= |
Call the top-level function by its name:
| In[389]:= |
Use the qualified name to call the function at a deeper level:
| In[390]:= |
Also use the qualified name to call the two functions at the same level in two modules:
| In[391]:= |
| In[392]:= |
Alternatively, create another PythonObject for one of the modules:
| In[393]:= |
| Out[393]= | ![]() |
In that context, the function name is unique and the function can be called just by its name:
| In[394]:= |
| In[395]:= |
The Span notation is internally converted to Python's slice to extract parts of list-like objects:
| In[396]:= |
| Out[396]= |
| In[397]:= |
| Out[397]= | ![]() |
| In[398]:= |
| Out[398]= |
When using the slice notation directly, keep in mind that Python's list indexes are zero-based:
| In[399]:= |
| Out[399]= |
Also, the start:end notation in Python does not include the right-end boundary:
| In[400]:= |
| Out[400]= |
Get the same elements with slice, using ExternalEvaluate or PythonObject:
| In[401]:= |
| Out[401]= |
| In[402]:= |
| Out[402]= | ![]() |
| In[403]:= |
| Out[403]= |
Although typically using "Part"[…] with Span in PythonObject gives the result equivalent to using Part[…] on the fetched expression, there are no out-of-range error messages for slicing in Python:
| In[404]:= |
| Out[404]= |
| In[405]:= |
| Out[405]= |
| In[406]:= |
PythonObject cannot be created by Python statements that do not return an object, such as statements defining a function:
| In[407]:= |
| In[408]:= |
| In[409]:= |
| Out[409]= |
Define the function using ExternalEvaluate and give the function name to PythonObject:
| In[410]:= |
| In[411]:= |
| Out[411]= | ![]() |
Use the object:
| In[412]:= |
| In[413]:= |
If "cmd" in PythonObject[session,"cmd"] contains multiple statements, all the statements are executed, but the PythonObject is currently created from the first statement:
| In[414]:= |
| In[415]:= |
| Out[415]= | ![]() |
| In[416]:= |
| Out[416]= |
This will be improved on in a future update; in the meantime, use separate statements for each object you want to create:
| In[417]:= |
| Out[417]= | ![]() |
| In[418]:= |
If a Python object cannot be created, PythonObject might return a syntax error:
| In[419]:= |
| In[420]:= |
| Out[420]= |
Use ExternalEvaluate if you want to import the package manually:
| In[421]:= |
| In[422]:= |
The arithmetic operation Times on Python objects follows the Python conventions for the mul method, which is object dependent:
| In[423]:= |
| In[424]:= |
| Out[424]= | ![]() |
| In[425]:= |
| Out[425]= |
| In[426]:= |
| Out[426]= | ![]() |
| In[427]:= |
| Out[427]= |
Use nympy arrays to make Times behave as expected in Wolfram Language:
| In[428]:= |
| Out[428]= | ![]() |
| In[429]:= |
| Out[429]= |
Similarly, for Plus:
| In[430]:= |
| Out[430]= |
| In[431]:= |
| Out[431]= |
| In[432]:= |
Orderless operations, like Plus and Times, send operands to Python sorted in canonical order:
| In[433]:= |
| In[434]:= |
| Out[434]= | ![]() |
| In[435]:= |
| Out[435]= | ![]() |
| In[436]:= |
| Out[436]= |
Use "Evaluate" to send the operands in the desired order:
| In[437]:= |
| Out[437]= |
| In[438]:= |
For comparison operators, which do not have the Orderless attribute, the first operand must be a Python object:
| In[439]:= |
| In[440]:= |
| Out[440]= | ![]() |
| In[441]:= |
| Out[441]= |
The mathematically equivalent operation in the reverse order does not evaluate:
| In[442]:= |
| Out[442]= | ![]() |
Define the first object in Python to have the comparison evaluated:
| In[443]:= |
| Out[443]= |
| In[444]:= |
Bitwise operators in Python evaluate the operands:
| In[445]:= |
| In[446]:= |
| Out[446]= | ![]() |
| In[447]:= |
| Out[447]= |
Use the keyword or with "Evaluate" to get the Boolean result:
| In[448]:= |
| Out[448]= |
| In[449]:= |
If a Python function does not specify default values for one or more named arguments, options that represent such arguments become mandatory:
| In[450]:= |
| Out[450]= |
| In[451]:= | ![]() |
| In[452]:= |
| Out[452]= | ![]() |
The function fails if the mandatory argument is not supplied:
| In[453]:= |
| Out[453]= |
Specify the mandatory argument:
| In[454]:= |
| Out[454]= |
| In[455]:= |
Signatures of some callable objects can contain Missing elements:
| In[456]:= |
| In[457]:= |
| Out[457]= | ![]() |
| In[458]:= |
| Out[458]= | ![]() |
You can still use such objects, but need to obtain their signatures manually:
| In[459]:= |
| In[460]:= |
| Out[460]= |
Also, for such objects, you cannot use standard Wolfram Language options and should instead supply optional arguments:
| In[461]:= |
| Out[461]= |
| In[462]:= |
| Out[462]= |
| In[463]:= |
Wolfram Language 13.0 (December 2021) or above
This work is licensed under a Creative Commons Attribution 4.0 International License