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. |
"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 |
"Session" | ExternalSessionObject |
"PythonReference" | a reference to the Python-side object |
"ReferenceValid" | whether the reference corresponds to an active Python session |
"Information" | Python-side information |
"FullInformation" | recursive information for modules |
"RawInformation" | Python-side help |
"Configuration" | configuration |
"IsCallable" | callable object |
"IsModule" | module |
"IsClass" | class |
"IsFunction" | function |
"IsBuiltin" | built-in function |
"Arguments" | arguments |
"Options" | options |
"VarArgs" | whether the function accepts 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 single Python object:
In[21]:= | ![]() |
Out[21]= | ![]() |
In[22]:= | ![]() |
Multiple objects:
In[23]:= | ![]() |
Out[23]= | ![]() |
In[24]:= | ![]() |
Components of a Python list:
In[25]:= | ![]() |
Out[25]= | ![]() |
In[26]:= | ![]() |
Python dictionaries containing objects:
In[27]:= | ![]() |
In[28]:= | ![]() |
Out[28]= | ![]() |
In[29]:= | ![]() |
Create a PythonObject[…] only for those objects that do not have a Wolfram Language representation:
In[30]:= | ![]() |
In[31]:= | ![]() |
In[32]:= | ![]() |
Out[32]= | ![]() |
Create Python objects for all objects, even the simplest:
In[33]:= | ![]() |
Out[33]= | ![]() |
In[34]:= | ![]() |
Get a list of items in a Python tuple:
In[35]:= | ![]() |
In[36]:= | ![]() |
Out[36]= | ![]() |
In[37]:= | ![]() |
Out[37]= | ![]() |
Get the tuple as PythonObject:
In[38]:= | ![]() |
Out[38]= | ![]() |
Get the tuple components:
In[39]:= | ![]() |
Out[39]= | ![]() |
In[40]:= | ![]() |
Define an instance object in Python and access its Python-side property:
In[41]:= | ![]() |
In[42]:= | ![]() |
Out[42]= | ![]() |
In[43]:= | ![]() |
Out[43]= | ![]() |
In[44]:= | ![]() |
Out[44]= | ![]() |
Assign a new value to the property:
In[45]:= | ![]() |
Out[45]= | ![]() |
In[46]:= | ![]() |
Out[46]= | ![]() |
Call a method:
In[47]:= | ![]() |
Out[47]= | ![]() |
Alternatively, define a Python object referring to the class and construct an instance on the Wolfram Language side:
In[48]:= | ![]() |
Out[48]= | ![]() |
In[49]:= | ![]() |
Out[49]= | ![]() |
The new value of the instance variable:
In[50]:= | ![]() |
Out[50]= | ![]() |
Call a member function:
In[51]:= | ![]() |
Out[51]= | ![]() |
In[52]:= | ![]() |
Get a part of a list-like object without bringing the object from Python:
In[53]:= | ![]() |
Out[53]= | ![]() |
In[54]:= | ![]() |
Out[54]= | ![]() |
In[55]:= | ![]() |
Out[55]= | ![]() |
Get spans of elements:
In[56]:= | ![]() |
Out[56]= | ![]() |
In[57]:= | ![]() |
Out[57]= | ![]() |
In[58]:= | ![]() |
Out[58]= | ![]() |
In[59]:= | ![]() |
Out[59]= | ![]() |
Get all parts:
In[60]:= | ![]() |
Out[60]= | ![]() |
In[61]:= | ![]() |
Get a part of a multi-dimensional Python array:
In[62]:= | ![]() |
Out[62]= | ![]() |
In[63]:= | ![]() |
Out[63]= | ![]() |
In[64]:= | ![]() |
Out[64]= | ![]() |
Get a span of elements:
In[65]:= | ![]() |
Out[65]= | ![]() |
In[66]:= | ![]() |
Define a Python list object:
In[67]:= | ![]() |
Out[67]= | ![]() |
In[68]:= | ![]() |
Out[68]= | ![]() |
Assign a new value to a part of the list:
In[69]:= | ![]() |
Out[69]= | ![]() |
In[70]:= | ![]() |
Out[70]= | ![]() |
Assign a new value to a span of elements:
In[71]:= | ![]() |
Out[71]= | ![]() |
In[72]:= | ![]() |
Out[72]= | ![]() |
In[73]:= | ![]() |
Define a Python class whose variable is an instance of another class:
In[74]:= | ![]() |
Out[74]= | ![]() |
In[75]:= | ![]() |
Out[75]= | ![]() |
In[76]:= | ![]() |
Out[76]= | ![]() |
The value of the variable c, "a.b.c" in the Python notation:
In[77]:= | ![]() |
Out[77]= | ![]() |
Assign the value to c:
In[78]:= | ![]() |
Out[78]= | ![]() |
Check the value on the Python side:
In[79]:= | ![]() |
Out[79]= | ![]() |
In[80]:= | ![]() |
Out[80]= | ![]() |
Alternatively:
In[81]:= | ![]() |
Out[81]= | ![]() |
In[82]:= | ![]() |
Define a Python object:
In[83]:= | ![]() |
Out[83]= | ![]() |
Evaluate a Python code referencing the object:
In[84]:= | ![]() |
Equivalently:
In[85]:= | ![]() |
In[86]:= | ![]() |
Supply arguments to be converted to Python expressions:
In[87]:= | ![]() |
String arguments are passed verbatim, without extra quotation marks:
In[88]:= | ![]() |
Escape quotation marks to pass an expression as a string:
In[89]:= | ![]() |
Out[89]= | ![]() |
Or use single quotes inside the string:
In[90]:= | ![]() |
Out[90]= | ![]() |
In[91]:= | ![]() |
Interactively defined object:
In[92]:= | ![]() |
Out[92]= | ![]() |
In[93]:= | ![]() |
A module in Python's standard library:
In[94]:= | ![]() |
Out[94]= | ![]() |
In[95]:= | ![]() |
A class in the standard library:
In[96]:= | ![]() |
Out[96]= | ![]() |
In[97]:= | ![]() |
An instance of a class in the standard library:
In[98]:= | ![]() |
Out[98]= | ![]() |
In[99]:= | ![]() |
A function in the standard library:
In[100]:= | ![]() |
Out[100]= | ![]() |
In[101]:= | ![]() |
A built-in function:
In[102]:= | ![]() |
Out[102]= | ![]() |
In[103]:= | ![]() |
An installable package, with its classes and functions:
In[104]:= | ![]() |
In[105]:= | ![]() |
Out[105]= | ![]() |
In[106]:= | ![]() |
Out[106]= | ![]() |
In[107]:= | ![]() |
Out[107]= | ![]() |
Uninstall when the package is no longer needed:
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 the 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 the 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]:= | ![]() |
A class:
In[167]:= | ![]() |
In[168]:= | ![]() |
Out[168]= | ![]() |
In[169]:= | ![]() |
Out[169]= | ![]() |
Create an instance of the class:
In[170]:= | ![]() |
Out[170]= | ![]() |
In[171]:= | ![]() |
A callable instance of the class:
In[172]:= | ![]() |
In[173]:= | ![]() |
Out[173]= | ![]() |
In[174]:= | ![]() |
Out[174]= | ![]() |
Call the instance:
In[175]:= | ![]() |
Out[175]= | ![]() |
In[176]:= | ![]() |
A function:
In[177]:= | ![]() |
In[178]:= | ![]() |
Out[178]= | ![]() |
In[179]:= | ![]() |
Out[179]= | ![]() |
Call the function:
In[180]:= | ![]() |
Out[180]= | ![]() |
In[181]:= | ![]() |
Call a function defined in a module via the parent module object:
In[182]:= | ![]() |
In[183]:= | ![]() |
Out[183]= | ![]() |
In[184]:= | ![]() |
In[185]:= | ![]() |
Out[185]= | ![]() |
In[186]:= | ![]() |
Out[186]= | ![]() |
Alternatively, define a callable function object and call it directly:
In[187]:= | ![]() |
Out[187]= | ![]() |
In[188]:= | ![]() |
Out[188]= | ![]() |
In[189]:= | ![]() |
Out[189]= | ![]() |
Clean up:
In[190]:= | ![]() |
Out[190]= | ![]() |
In[191]:= | ![]() |
Define a Python class:
In[192]:= | ![]() |
In[193]:= | ![]() |
Out[193]= | ![]() |
Create a Python object referring to the class:
In[194]:= | ![]() |
Out[194]= | ![]() |
In PythonObject, optional arguments of a callable Python object become standard Wolfram Language options:
In[195]:= | ![]() |
Out[195]= | ![]() |
Create an instance and check its variables:
In[196]:= | ![]() |
Out[196]= | ![]() |
In[197]:= | ![]() |
Out[197]= | ![]() |
For comparison, specify the optional argument in the Python code and check the new instance variables:
In[198]:= | ![]() |
Out[198]= | ![]() |
In[199]:= | ![]() |
Define a Python class in which the constructor takes a function as an argument:
In[200]:= | ![]() |
In[201]:= | ![]() |
Out[201]= | ![]() |
Create a Python object referring to the class:
In[202]:= | ![]() |
Out[202]= | ![]() |
To create an instance of the class, define a function on the Python side:
In[203]:= | ![]() |
Out[203]= | ![]() |
Supply the ExternalFunction object to the class constructor::
In[204]:= | ![]() |
Out[204]= | ![]() |
Invoke a method of the class:
In[205]:= | ![]() |
Alternatively, initialize an instance with a PythonObject:
In[206]:= | ![]() |
Out[206]= | ![]() |
In[207]:= | ![]() |
Out[207]= | ![]() |
Invoke the method:
In[208]:= | ![]() |
In[209]:= | ![]() |
Define a Python function which expects a specific type of a Python object, for instance, the enum Color, as an option:
In[210]:= | ![]() |
In[211]:= | ![]() |
In[212]:= | ![]() |
Out[212]= | ![]() |
A Python object for the enum:
In[213]:= | ![]() |
Out[213]= | ![]() |
Supply an enum value to the function:
In[214]:= | ![]() |
Out[214]= | ![]() |
In[215]:= | ![]() |
Define a class in Python:
In[216]:= | ![]() |
In[217]:= | ![]() |
Create a Python object referring to the class:
In[218]:= | ![]() |
Out[218]= | ![]() |
Call a method function and get the instance variable using strings as names:
In[219]:= | ![]() |
Out[204]= | ![]() |
Alternatively, use symbols as names:
In[220]:= | ![]() |
Out[206]= | ![]() |
In[221]:= | ![]() |
Define a Python package with a variable and a function that both return lists:
In[222]:= | ![]() |
In[223]:= | ![]() |
In[224]:= | ![]() |
Out[224]= | ![]() |
Variable as a list:
In[225]:= | ![]() |
Out[225]= | ![]() |
Use the policy argument to keep the list on the Python side:
In[226]:= | ![]() |
Out[226]= | ![]() |
Function result as a list:
In[227]:= | ![]() |
Out[227]= | ![]() |
Create a Python-side object using the policy argument:
In[228]:= | ![]() |
Out[228]= | ![]() |
Callable:
In[229]:= | ![]() |
Out[229]= | ![]() |
In[230]:= | ![]() |
Out[230]= | ![]() |
Invoke the callable with the policy argument:
In[231]:= | ![]() |
Out[231]= | ![]() |
In[232]:= | ![]() |
Create a Python object for a list:
In[233]:= | ![]() |
Out[233]= | ![]() |
Bring the result to the Wolfram Language:
In[234]:= | ![]() |
Out[234]= | ![]() |
In[235]:= | ![]() |
Deploy a temporary package that defines a class with a class method:
In[236]:= | ![]() |
In[237]:= | ![]() |
In[238]:= | ![]() |
Out[238]= | ![]() |
Create a Python object referring to the class:
In[239]:= | ![]() |
Out[239]= | ![]() |
Create a Python object referring to the class method:
In[240]:= | ![]() |
Out[240]= | ![]() |
Call the method and notice that the class object is passed to the class method as the first argument:
In[241]:= | ![]() |
Out[241]= | ![]() |
In[242]:= | ![]() |
Create a decorated function in a Python session:
In[243]:= | ![]() |
Out[243]= | ![]() |
In[244]:= | ![]() |
Out[244]= | ![]() |
The Python object representing the function:
In[245]:= | ![]() |
Out[245]= | ![]() |
The signature of the decorated function is inferred correctly, despite it being masked by the decoration:
In[246]:= | ![]() |
Out[246]= | ![]() |
Call the function with the mandatory and optional arguments:
In[247]:= | ![]() |
Out[247]= | ![]() |
In[248]:= | ![]() |
Out[248]= | ![]() |
In[249]:= | ![]() |
Without initialization, PythonObject fails for this command because the required package is not available in the current session:
In[250]:= | ![]() |
In[251]:= | ![]() |
Out[251]= | ![]() |
In fact, the package is not even installed:
In[252]:= | ![]() |
Out[252]= | ![]() |
With the default setting Initialization→Automatic, the package is automatically installed and imported:
In[253]:= | ![]() |
Out[253]= | ![]() |
The package is now installed:
In[254]:= | ![]() |
Out[254]= | ![]() |
Uninstall to clean up:
In[255]:= | ![]() |
Out[255]= | ![]() |
In[256]:= | ![]() |
Specify a custom initialization:
In[257]:= | ![]() |
Out[257]= | ![]() |
Use the new object:
In[258]:= | ![]() |
Out[258]= | ![]() |
Uninstall the package and close the session to clean up:
In[259]:= | ![]() |
Out[259]= | ![]() |
In[260]:= | ![]() |
Without configuration, the raw Python's uuid4() function returns an instance of the UUID class:
In[261]:= | ![]() |
Out[261]= | ![]() |
In[262]:= | ![]() |
Out[262]= | ![]() |
In[263]:= | ![]() |
Out[263]= | ![]() |
In[264]:= | ![]() |
Specify a configuration defined in a resource function:
In[265]:= | ![]() |
Out[265]= | ![]() |
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[266]:= | ![]() |
Out[266]= | ![]() |
In[267]:= | ![]() |
Out[267]= | ![]() |
In[268]:= | ![]() |
Create a PythonObject to efficiently reuse data on the Python side, even when you do not have access to the Python code:
In[269]:= | ![]() |
Execute some code that defines a big object and keep a reference to that object:
In[270]:= | ![]() |
In[271]:= | ![]() |
Out[271]= | ![]() |
Use the object:
In[272]:= | ![]() |
Out[272]= | ![]() |
In[273]:= | ![]() |
Make a function defined in an external module available in your Wolfram Language session:
In[274]:= | ![]() |
Out[274]= | ![]() |
The function signature:
In[275]:= | ![]() |
Out[275]= | ![]() |
Call the function:
In[276]:= | ![]() |
Out[276]= | ![]() |
Information about the returned object:
In[277]:= | ![]() |
Out[277]= | ![]() |
The value of one of the available variables:
In[278]:= | ![]() |
Out[278]= | ![]() |
Another variable refers to a more complex object:
In[279]:= | ![]() |
Out[279]= | ![]() |
In[280]:= | ![]() |
Out[280]= | ![]() |
Get the signature of one of the methods of the new object:
In[281]:= | ![]() |
Out[281]= | ![]() |
Call the method several times:
In[282]:= | ![]() |
Out[282]= | ![]() |
Uninstall the package to clean up:
In[283]:= | ![]() |
Out[283]= | ![]() |
In[284]:= | ![]() |
Create a Python object for an external package, for instance, Topoly:
In[285]:= | ![]() |
Out[285]= | ![]() |
Explore its contents:
In[286]:= | ![]() |
Out[286]= | ![]() |
Find the signature of a function from the package:
In[287]:= | ![]() |
Out[287]= | ![]() |
Prepare and plot a list of coordinates that represent a 51knot:
In[288]:= | ![]() |
In[289]:= | ![]() |
Out[289]= | ![]() |
Call the Topoly function and display its result, in this case, a plot of the fingerprint matrix for the coordinates of the knot:
In[290]:= | ![]() |
In[291]:= | ![]() |
In[292]:= | ![]() |
Out[292]= | ![]() |
In[293]:= | ![]() |
Some common properties:
In[294]:= | ![]() |
Out[294]= | ![]() |
In[295]:= | ![]() |
Out[295]= | ![]() |
Curated properties:
In[296]:= | ![]() |
Out[296]= | ![]() |
In[297]:= | ![]() |
Contents of a package:
In[298]:= | ![]() |
Out[298]= | ![]() |
In[299]:= | ![]() |
Out[299]= | ![]() |
Recurse into submodules:
In[300]:= | ![]() |
Out[300]= | ![]() |
All modules in the package:
In[301]:= | ![]() |
Out[301]= | ![]() |
Contents of a module:
In[302]:= | ![]() |
Out[302]= | ![]() |
In[303]:= | ![]() |
Out[303]= | ![]() |
In[304]:= | ![]() |
Information on a class, including the class variable and the methods:
In[305]:= | ![]() |
In[306]:= | ![]() |
Out[306]= | ![]() |
In[307]:= | ![]() |
Out[307]= | ![]() |
In[308]:= | ![]() |
Out[308]= | ![]() |
Information on a class instance, including the class and instance variables and the methods:
In[309]:= | ![]() |
Out[309]= | ![]() |
In[310]:= | ![]() |
Out[310]= | ![]() |
In[311]:= | ![]() |
Obtain the signature of a callable object:
In[312]:= | ![]() |
Out[312]= | ![]() |
In[313]:= | ![]() |
Out[313]= | ![]() |
In[314]:= | ![]() |
Access Python's "built-ins", mathematical functions defined by the C standard:
In[315]:= | ![]() |
Out[315]= | ![]() |
In[316]:= | ![]() |
Out[316]= | ![]() |
In[317]:= | ![]() |
Out[317]= | ![]() |
In[318]:= | ![]() |
Out[318]= | ![]() |
In[319]:= | ![]() |
Even if not listed in "Properties", native Python attributes are also accessible:
In[320]:= | ![]() |
In[321]:= | ![]() |
Out[321]= | ![]() |
In[322]:= | ![]() |
Out[322]= | ![]() |
In[323]:= | ![]() |
Similarly, you can use double underscore methods ("dunders"):
In[324]:= | ![]() |
Out[324]= | ![]() |
In[325]:= | ![]() |
Out[325]= | ![]() |
In[326]:= | ![]() |
Out[326]= | ![]() |
In[327]:= | ![]() |
Get the Python-side help:
In[328]:= | ![]() |
In[329]:= | ![]() |
In[330]:= | ![]() |
Use the "PythonReference" property to access the object on the Python side with ExternalEvaluate:
In[331]:= | ![]() |
Out[331]= | ![]() |
In[332]:= | ![]() |
Out[332]= | ![]() |
The explicit "PythonReference" specification can be omitted since it is the default property:
In[333]:= | ![]() |
Out[333]= | ![]() |
In[334]:= | ![]() |
Out[334]= | ![]() |
In[335]:= | ![]() |
"PythonReference" becomes stale if you delete the Python object:
In[336]:= | ![]() |
Out[332]= | ![]() |
In[337]:= | ![]() |
Out[334]= | ![]() |
"PythonReference" also stales if you close the session in which it is defined:
In[338]:= | ![]() |
Out[311]= | ![]() |
PythonObject is similar to ExternalObject returned by ExternalEvaluate:
In[339]:= | ![]() |
In[340]:= | ![]() |
Out[340]= | ![]() |
In[341]:= | ![]() |
Out[341]= | ![]() |
PythonObject is also similar to ExternalFunction object that can be defined to call a Python function:
In[342]:= | ![]() |
Out[342]= | ![]() |
In[343]:= | ![]() |
Out[343]= | ![]() |
In[344]:= | ![]() |
Out[344]= | ![]() |
Call the two functions:
In[345]:= | ![]() |
In[346]:= | ![]() |
Out[346]= | ![]() |
The major difference between the produced objects is that the contents of the latter are accessible directly in the Wolfram Language without any Python programming:
In[347]:= | ![]() |
Out[347]= | ![]() |
In[348]:= | ![]() |
Out[348]= | ![]() |
Uninstall the package to clean up:
In[349]:= | ![]() |
Out[349]= | ![]() |
In[350]:= | ![]() |
For callable objects, PythonObject[…][All] is used only for calling the object and otherwise returns unevaluated:
In[351]:= | ![]() |
Out[351]= | ![]() |
In[352]:= | ![]() |
Out[352]= | ![]() |
In[353]:= | ![]() |
Out[353]= | ![]() |
In[354]:= | ![]() |
Create and deploy a Python package with one unique function name and two identically named functions in two modules:
In[355]:= | ![]() |
In[356]:= | ![]() |
Out[356]= | ![]() |
Call the unique function simply by its name, given as a string or a symbol:
In[357]:= | ![]() |
However, trying to use non-uniquely named functions the same way gives an error:
In[358]:= | ![]() |
Out[358]= | ![]() |
Use the qualified dot notation for strings, or use symbols in proper contexts:
In[359]:= | ![]() |
In[360]:= | ![]() |
Alternatively, create another PythonObject for one of the modules:
In[361]:= | ![]() |
Out[361]= | ![]() |
In that context, the function name is unique and the function can be called just by its name:
In[362]:= | ![]() |
In[363]:= | ![]() |
The Span notation is internally converted to Python's slice to extract parts of list-like objects:
In[364]:= | ![]() |
Out[364]= | ![]() |
In[365]:= | ![]() |
Out[365]= | ![]() |
In[366]:= | ![]() |
Out[366]= | ![]() |
When using the slice notation directly, keep in mind that Python's list indexes are zero-based:
In[367]:= | ![]() |
Out[367]= | ![]() |
Also, the start:end notation in Python does not include the right-end boundary:
In[368]:= | ![]() |
Out[368]= | ![]() |
Get the same elements with slice, using ExternalEvaluate or PythonObject:
In[369]:= | ![]() |
Out[369]= | ![]() |
In[370]:= | ![]() |
Out[370]= | ![]() |
In[371]:= | ![]() |
Out[371]= | ![]() |
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[372]:= | ![]() |
Out[372]= | ![]() |
In[373]:= | ![]() |
Out[373]= | ![]() |
In[374]:= | ![]() |
PythonObject cannot be created by Python statements that do not return an object, such as statements defining a function:
In[375]:= | ![]() |
In[376]:= | ![]() |
In[377]:= | ![]() |
Out[377]= | ![]() |
Define the function using ExternalEvaluate and give the function name to PythonObject:
In[378]:= | ![]() |
In[379]:= | ![]() |
Out[379]= | ![]() |
Use the object:
In[380]:= | ![]() |
In[381]:= | ![]() |
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[382]:= | ![]() |
In[383]:= | ![]() |
Out[383]= | ![]() |
In[384]:= | ![]() |
Out[384]= | ![]() |
We plan to improve on that in a future update; in the meantime, use separate statements for each object you want to create:
In[385]:= | ![]() |
Out[385]= | ![]() |
In[386]:= | ![]() |
If a Python object cannot be created, PythonObject might return a syntax error:
In[387]:= | ![]() |
In[388]:= | ![]() |
Out[388]= | ![]() |
Use ExternalEvaluate if you want to import the package manually:
In[389]:= | ![]() |
In[390]:= | ![]() |
The arithmetic operation Times on Python objects follows the Python conventions for the mul method, which is object-dependent:
In[391]:= | ![]() |
In[392]:= | ![]() |
Out[392]= | ![]() |
In[393]:= | ![]() |
Out[393]= | ![]() |
In[394]:= | ![]() |
Out[394]= | ![]() |
In[395]:= | ![]() |
Out[395]= | ![]() |
Use nympy arrays to make Times behave as expected in the Wolfram Language:
In[396]:= | ![]() |
Out[396]= | ![]() |
In[397]:= | ![]() |
Out[397]= | ![]() |
Similarly, for Plus:
In[398]:= | ![]() |
Out[398]= | ![]() |
In[399]:= | ![]() |
Out[399]= | ![]() |
In[400]:= | ![]() |
Orderless operations, like Plus and Times, send operands to Python sorted in canonical order:
In[401]:= | ![]() |
In[402]:= | ![]() |
Out[402]= | ![]() |
In[403]:= | ![]() |
Out[403]= | ![]() |
In[404]:= | ![]() |
Out[404]= | ![]() |
Use "Evaluate" to send the operands in the desired order:
In[405]:= | ![]() |
Out[405]= | ![]() |
In[406]:= | ![]() |
For comparison operators, which do not have the Orderless attribute, the first operand must be a Python object:
In[407]:= | ![]() |
In[408]:= | ![]() |
Out[408]= | ![]() |
In[409]:= | ![]() |
Out[409]= | ![]() |
The mathematically equivalent operation in the reverse order does not evaluate:
In[410]:= | ![]() |
Out[410]= | ![]() |
Define the first object in Python to have the comparison evaluated:
In[411]:= | ![]() |
Out[411]= | ![]() |
In[412]:= | ![]() |
Bitwise operators in Python evaluate the operands:
In[413]:= | ![]() |
In[414]:= | ![]() |
Out[414]= | ![]() |
In[415]:= | ![]() |
Out[415]= | ![]() |
Use the keyword or with "Evaluate" to get the Boolean result:
In[416]:= | ![]() |
Out[416]= | ![]() |
In[417]:= | ![]() |
If a Python function does not specify default values for one or more named arguments, options that represent such arguments become mandatory:
In[418]:= | ![]() |
Out[418]= | ![]() |
In[419]:= | ![]() |
In[420]:= | ![]() |
Out[420]= | ![]() |
The function fails if the mandatory argument is not supplied:
In[421]:= | ![]() |
Out[421]= | ![]() |
Specify the mandatory argument:
In[422]:= | ![]() |
Out[422]= | ![]() |
In[423]:= | ![]() |
Signatures of some callable objects can contain Missing elements:
In[424]:= | ![]() |
In[425]:= | ![]() |
Out[425]= | ![]() |
In[426]:= | ![]() |
Out[426]= | ![]() |
You can still use such objects, but need to obtain their signatures manually:
In[427]:= | ![]() |
In[428]:= | ![]() |
Out[428]= | ![]() |
Also, for such objects, you cannot use standard Wolfram Language options and should rather supply optional arguments:
In[429]:= | ![]() |
Out[429]= | ![]() |
In[430]:= | ![]() |
Out[430]= | ![]() |
In[431]:= | ![]() |
This work is licensed under a Creative Commons Attribution 4.0 International License