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 |
None | no initialization |
ResourceFunction["function"] | 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 |
"Functions" | functions |
"Classes" | classes |
"Modules" | modules |
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]:= |
![]() |
Create a PythonObject[…] only for those objects that do not have a Wolfram Language representation:
In[25]:= |
![]() |
In[26]:= |
![]() |
In[27]:= |
![]() |
Out[27]= |
![]() |
Create Python objects for all objects, even the simplest:
In[28]:= |
![]() |
Out[28]= |
![]() |
In[29]:= |
![]() |
Define an instance object in Python and access its Python-side property:
In[30]:= |
![]() |
In[31]:= |
![]() |
Out[31]= |
![]() |
In[32]:= |
![]() |
Out[32]= |
![]() |
In[33]:= |
![]() |
Out[33]= |
![]() |
Assign a new value to the property:
In[34]:= |
![]() |
Out[34]= |
![]() |
In[35]:= |
![]() |
Out[35]= |
![]() |
Call a method:
In[36]:= |
![]() |
Out[36]= |
![]() |
Alternatively, define a Python object referring to the class and construct an instance on the Wolfram-Language side:
In[37]:= |
![]() |
Out[37]= |
![]() |
In[38]:= |
![]() |
Out[38]= |
![]() |
The new value of the instance variable:
In[39]:= |
![]() |
Out[39]= |
![]() |
Call a member function:
In[40]:= |
![]() |
Out[40]= |
![]() |
In[41]:= |
![]() |
Interactively defined object:
In[42]:= |
![]() |
Out[42]= |
![]() |
In[43]:= |
![]() |
A module in Python’s standard library:
In[44]:= |
![]() |
Out[44]= |
![]() |
In[45]:= |
![]() |
A class in the standard library:
In[46]:= |
![]() |
Out[46]= |
![]() |
In[47]:= |
![]() |
An instance of a class in the standard library:
In[48]:= |
![]() |
Out[48]= |
![]() |
In[49]:= |
![]() |
A function in the standard library:
In[50]:= |
![]() |
Out[50]= |
![]() |
In[51]:= |
![]() |
A built-in function:
In[52]:= |
![]() |
Out[52]= |
![]() |
In[53]:= |
![]() |
An installable package, with its classes and functions:
In[54]:= |
![]() |
In[55]:= |
![]() |
Out[55]= |
![]() |
In[56]:= |
![]() |
Out[56]= |
![]() |
In[57]:= |
![]() |
Out[57]= |
![]() |
Uninstall when the package no longer needed:
In[58]:= |
![]() |
Out[58]= |
![]() |
In[59]:= |
![]() |
A class:
In[60]:= |
![]() |
In[61]:= |
![]() |
Out[61]= |
![]() |
In[62]:= |
![]() |
Out[62]= |
![]() |
Create an instance of the class:
In[63]:= |
![]() |
Out[63]= |
![]() |
In[64]:= |
![]() |
A callable instance of the class:
In[65]:= |
![]() |
In[66]:= |
![]() |
Out[66]= |
![]() |
In[67]:= |
![]() |
Out[67]= |
![]() |
Call the instance:
In[68]:= |
![]() |
Out[68]= |
![]() |
In[69]:= |
![]() |
A function:
In[70]:= |
![]() |
In[71]:= |
![]() |
Out[71]= |
![]() |
In[72]:= |
![]() |
Out[72]= |
![]() |
Call the function:
In[73]:= |
![]() |
Out[73]= |
![]() |
In[74]:= |
![]() |
Call a function defined in a module via the parent module object:
In[75]:= |
![]() |
In[76]:= |
![]() |
Out[76]= |
![]() |
In[77]:= |
![]() |
In[78]:= |
![]() |
Out[78]= |
![]() |
In[79]:= |
![]() |
Out[79]= |
![]() |
Alternatively, define a callable function object and call it directly:
In[80]:= |
![]() |
Out[80]= |
![]() |
In[81]:= |
![]() |
Out[81]= |
![]() |
In[82]:= |
![]() |
Out[82]= |
![]() |
Clean up:
In[83]:= |
![]() |
Out[83]= |
![]() |
In[84]:= |
![]() |
Define a Python class:
In[85]:= |
![]() |
In[86]:= |
![]() |
Out[86]= |
![]() |
Create a Python object referring to the class:
In[87]:= |
![]() |
Out[87]= |
![]() |
In PythonObject, optional arguments of a callable Python object become standard Wolfram Language options:
In[88]:= |
![]() |
Out[88]= |
![]() |
Create an instance and check its variables:
In[89]:= |
![]() |
Out[89]= |
![]() |
In[90]:= |
![]() |
Out[90]= |
![]() |
For comparison, specify the optional argument in the Python code and check the new instance variables:
In[91]:= |
![]() |
Out[91]= |
![]() |
In[92]:= |
![]() |
Define a Python class in which the constructor takes a function as an argument:
In[93]:= |
![]() |
In[94]:= |
![]() |
Out[94]= |
![]() |
Create a Python object referring to the class:
In[95]:= |
![]() |
Out[95]= |
![]() |
To create an an instance of the class, define a function on the Python side:
In[96]:= |
![]() |
Out[96]= |
![]() |
Supply the ExternalFunction object to the class constructor::
In[97]:= |
![]() |
Out[97]= |
![]() |
Invoke a method of the class:
In[98]:= |
![]() |
Alternatively, initialize an instance with a PythonObject:
In[99]:= |
![]() |
Out[99]= |
![]() |
In[100]:= |
![]() |
Out[100]= |
![]() |
Invoke the method:
In[101]:= |
![]() |
In[102]:= |
![]() |
Define a Python function which expects a specific type of a Python object, for instance, the enum Color, as an option:
In[103]:= |
![]() |
In[104]:= |
![]() |
In[105]:= |
![]() |
Out[105]= |
![]() |
A Python object for the enum:
In[106]:= |
![]() |
Out[106]= |
![]() |
Supply an enum value to the function:
In[107]:= |
![]() |
Out[107]= |
![]() |
In[108]:= |
![]() |
Define a class in Python and a corresponding PythonObject:
In[109]:= |
![]() |
In[110]:= |
![]() |
Create a Python object referring to the class:
In[111]:= |
![]() |
Out[111]= |
![]() |
Call a method function and get the instance variable using strings as names:
In[112]:= |
![]() |
Out[112]= |
![]() |
Alternatively, use symbols as names:
In[113]:= |
![]() |
Out[113]= |
![]() |
In[114]:= |
![]() |
Without initialization, PythonObject fails for this command because the required package is not available in the current session:
In[115]:= |
![]() |
In[116]:= |
![]() |
Out[116]= |
![]() |
In fact, the package is not even installed:
In[117]:= |
![]() |
Out[117]= |
![]() |
With the default setting Initialization→Automatic, the package is automatically installed and imported:
In[118]:= |
![]() |
Out[118]= |
![]() |
The package is now installed:
In[119]:= |
![]() |
Out[119]= |
![]() |
Uninstall to clean up:
In[120]:= |
![]() |
Out[120]= |
![]() |
In[121]:= |
![]() |
Specify a custom initialization:
In[122]:= |
![]() |
Out[122]= |
![]() |
Use the new object:
In[123]:= |
![]() |
Out[123]= |
![]() |
Uninstall the package and close the session to clean up:
In[124]:= |
![]() |
Out[124]= |
![]() |
In[125]:= |
![]() |
Without configuration, the raw Python’s uuid4() function returns an instance of the UUID class:
In[126]:= |
![]() |
Out[126]= |
![]() |
In[127]:= |
![]() |
Out[127]= |
![]() |
In[128]:= |
![]() |
Out[128]= |
![]() |
In[129]:= |
![]() |
Specify a configuration defined in the resource function UUIDPythonObjectConfiguration:
In[130]:= |
![]() |
Out[130]= |
![]() |
The resource function changes the return value of the Python object to a string:
In[131]:= |
![]() |
Out[131]= |
![]() |
In[132]:= |
![]() |
Create a PythonObject to efficiently reuse data on the Python side, even when you do not have access to the Python code.
In[133]:= |
![]() |
Out[133]= |
![]() |
Execute some code that defines a big object and keep a reference to that object:
In[134]:= |
![]() |
In[135]:= |
![]() |
Out[135]= |
![]() |
Use the object:
In[136]:= |
![]() |
Out[136]= |
![]() |
In[137]:= |
![]() |
Make a function defined in an external module available in your Wolfram Language session:
In[138]:= |
![]() |
Out[138]= |
![]() |
The function signature:
In[139]:= |
![]() |
Out[139]= |
![]() |
Call the function:
In[140]:= |
![]() |
Out[140]= |
![]() |
Information about the returned object:
In[141]:= |
![]() |
Out[141]= |
![]() |
The value of one of the available variables:
In[142]:= |
![]() |
Out[142]= |
![]() |
Another variable refers to a more complex object:
In[143]:= |
![]() |
Out[143]= |
![]() |
In[144]:= |
![]() |
Out[144]= |
![]() |
Get the signature of one of the methods of the new object:
In[145]:= |
![]() |
Out[145]= |
![]() |
Call the method several times:
In[146]:= |
![]() |
Out[146]= |
![]() |
Uninstall the package to clean up:
In[147]:= |
![]() |
Out[147]= |
![]() |
In[148]:= |
![]() |
Create a Python object for an external package, for instance, Topoly:
In[149]:= |
![]() |
Out[149]= |
![]() |
Explore its contents:
In[150]:= |
![]() |
Out[150]= |
![]() |
Find the signature of a function from the package:
In[151]:= |
![]() |
Out[151]= |
![]() |
Prepare and plot a list of coordinates that represent a 51knot:
In[152]:= |
![]() |
In[153]:= |
![]() |
Out[153]= |
![]() |
Call the Topoly function and display its result, in this case, a plot of the fingerprint matrix for the coordinates of the knot:
In[154]:= |
![]() |
In[155]:= |
![]() |
In[156]:= |
![]() |
Out[156]= |
![]() |
In[157]:= |
![]() |
Some common properties:
In[158]:= |
![]() |
Out[158]= |
![]() |
In[159]:= |
![]() |
Out[159]= |
![]() |
Curated properties:
In[160]:= |
![]() |
Out[160]= |
![]() |
In[161]:= |
![]() |
Contents of a package:
In[162]:= |
![]() |
Out[162]= |
![]() |
In[163]:= |
![]() |
Out[163]= |
![]() |
Recurse into submodules:
In[164]:= |
![]() |
Out[164]= |
![]() |
All modules in the package:
In[165]:= |
![]() |
Out[165]= |
![]() |
Contents of a module:
In[166]:= |
![]() |
Out[166]= |
![]() |
In[167]:= |
![]() |
Out[167]= |
![]() |
In[168]:= |
![]() |
Information on a class, including the class variable and the methods:
In[169]:= |
![]() |
In[170]:= |
![]() |
Out[170]= |
![]() |
In[171]:= |
![]() |
Out[171]= |
![]() |
In[172]:= |
![]() |
Out[172]= |
![]() |
Information on a class instance, including the class and instance variables and the methods:
In[173]:= |
![]() |
Out[173]= |
![]() |
In[174]:= |
![]() |
Out[174]= |
![]() |
In[175]:= |
![]() |
Obtain the signature of a callable object:
In[176]:= |
![]() |
Out[176]= |
![]() |
In[177]:= |
![]() |
Out[177]= |
![]() |
In[178]:= |
![]() |
Access Python's "built-ins", mathematical functions defined by the C standard:
In[179]:= |
![]() |
Out[179]= |
![]() |
In[180]:= |
![]() |
Out[180]= |
![]() |
In[181]:= |
![]() |
Out[181]= |
![]() |
In[182]:= |
![]() |
Out[182]= |
![]() |
In[183]:= |
![]() |
Use the "PythonReference" property to access the object on the Python side with ExternalEvaluate:
In[184]:= |
![]() |
Out[184]= |
![]() |
In[185]:= |
![]() |
Out[185]= |
![]() |
The explicit "PythonReference" specification can be omitted since it is the default property:
In[186]:= |
![]() |
Out[186]= |
![]() |
In[187]:= |
![]() |
Out[187]= |
![]() |
In[188]:= |
![]() |
"PythonReference" becomes stale if you delete the Python object:
In[189]:= |
![]() |
Out[189]= |
![]() |
In[190]:= |
![]() |
Out[190]= |
![]() |
"PythonReference" also stales if you close the session in which it is defined:
In[191]:= |
![]() |
Out[191]= |
![]() |
Even if not listed in "Properties", native Python attributes are also accessible:
In[192]:= |
![]() |
In[193]:= |
![]() |
Out[193]= |
![]() |
In[194]:= |
![]() |
Out[194]= |
![]() |
In[195]:= |
![]() |
Get the Python-side help:
In[196]:= |
![]() |
In[197]:= |
![]() |
In[198]:= |
![]() |
PythonObject is similar to ExternalObject returned by ExternalEvaluate:
In[199]:= |
![]() |
In[200]:= |
![]() |
Out[200]= |
![]() |
In[201]:= |
![]() |
Out[201]= |
![]() |
PythonObject is also similar to ExternalFunction object defined to call a Python function:
In[202]:= |
![]() |
Out[202]= |
![]() |
In[203]:= |
![]() |
Out[203]= |
![]() |
In[204]:= |
![]() |
Out[204]= |
![]() |
Call the two functions:
In[205]:= |
![]() |
In[206]:= |
![]() |
Out[206]= |
![]() |
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[207]:= |
![]() |
Out[207]= |
![]() |
In[208]:= |
![]() |
Out[208]= |
![]() |
Uninstall the package to clean up:
In[209]:= |
![]() |
Out[209]= |
![]() |
In[210]:= |
![]() |
For callable objects, PythonObject[…][All] is used only for calling the object and otherwise returns unevaluated:
In[211]:= |
![]() |
Out[211]= |
![]() |
In[212]:= |
![]() |
Out[212]= |
![]() |
In[213]:= |
![]() |
Out[213]= |
![]() |
Create and deploy a Python package with one unique function name and two identically named functions in two modules:
In[214]:= |
![]() |
In[215]:= |
![]() |
Out[215]= |
![]() |
Call the unique function simply by its name, given as a string or a symbol:
In[216]:= |
![]() |
However, trying to use non-uniquely named functions the same way gives an error:
In[217]:= |
![]() |
Out[217]= |
![]() |
Use the qualified dot notation for strings, or use symbols in proper contexts:
In[218]:= |
![]() |
In[219]:= |
![]() |
Alternatively, create another PythonObject for one of the modules:
In[220]:= |
![]() |
Out[220]= |
![]() |
In that context, the function name is unique and the function can be called just by its name:
In[221]:= |
![]() |
In[222]:= |
![]() |
PythonObject cannot be created by Python statements that do not return an object:
In[223]:= |
![]() |
In[224]:= |
![]() |
In[225]:= |
![]() |
Out[225]= |
![]() |
Define the function using ExternalEvaluate and give the function name to PythonObject:
In[226]:= |
![]() |
In[227]:= |
![]() |
Out[227]= |
![]() |
Use the object:
In[228]:= |
![]() |
In[229]:= |
![]() |
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[230]:= |
![]() |
In[231]:= |
![]() |
Out[231]= |
![]() |
In[232]:= |
![]() |
Out[232]= |
![]() |
We plan to improve on that in a future update; in the meantime, use separate statements for each object you want to create:
In[233]:= |
![]() |
Out[233]= |
![]() |
In[234]:= |
![]() |
If a Python object cannot be created, PythonObject might return a syntax error:
In[235]:= |
![]() |
In[236]:= |
![]() |
Out[236]= |
![]() |
Use ExternalEvaluate if you want to import the package manually:
In[237]:= |
![]() |
In[238]:= |
![]() |
Signatures of some callable objects can contain Missing elements:
In[239]:= |
![]() |
In[240]:= |
![]() |
Out[240]= |
![]() |
In[241]:= |
![]() |
Out[241]= |
![]() |
You can still use such objects, but need to obtain their signatures manually:
In[242]:= |
![]() |
In[243]:= |
![]() |
Out[243]= |
![]() |
Also, for such objects, you cannot use standard Wolfram Language options and should rather supply optional arguments:
In[244]:= |
![]() |
Out[244]= |
![]() |
In[245]:= |
![]() |
Out[245]= |
![]() |
Alternatively, you can remove missing elements using the PythonObject API function "Signatures":
In[246]:= |
![]() |
In[247]:= |
![]() |
Out[247]= |
![]() |
Now the "log" function works the standard way:
In[248]:= |
![]() |
Out[248]= |
![]() |
In[249]:= |
![]() |
PythonObject is a work in progress. Not all packages are currently supported:
In[250]:= |
![]() |
Out[250]= |
![]() |
This work is licensed under a Creative Commons Attribution 4.0 International License