This question is about the compatibility of methods written in Python and ExternalEvaluate from Mathematica.
ExternalEvaluate allows Python functions to be called from Mathematica. For example, the function "surfaceArea" from sphere.py:
from math import pi
def surfaceArea(r):
return 4*pi*r**2
may be called from Mathematica in the following way
pySession = StartExternalSession["Python"];
ExternalEvaluate[pySession, File["sphere.py"]];
radius = 2.0;
ExternalEvaluate[pySession, "surfaceArea" -> {radius}]
Now, consider the sphere Python class (sphereClass.py)
from math import pi
class sphere(object):
def __init__(self,radius):
self.radius = radius
def volume(self):
return (4/3)*pi*self.radius**3
Aim: Evaluate volume from sphereClass.py at radius = 2.0 employing ExternalEvaluate.
ExternalEvaluate[s, "sp = sphere(" <> ExportString[radius, "PythonExpression"] <> ");sp.volume()"]
, which outputs33.5103
. $\endgroup$s
is yourpySession
. @user10181864 $\endgroup$