diff --git a/mathics/builtin/inout.py b/mathics/builtin/inout.py index f175ff397..4c614ef77 100644 --- a/mathics/builtin/inout.py +++ b/mathics/builtin/inout.py @@ -439,9 +439,14 @@ class PythonForm(Builtin): def apply_python(self, expr, evaluation) -> Expression: "MakeBoxes[expr_, PythonForm]" + def build_python_form(expr): + if isinstance(expr, Symbol): + return expr.to_sympy() + return expr.to_python() + try: # from trepan.api import debug; debug() - python_equivalent = expr.to_python(python_form=True) + python_equivalent = build_python_form(expr) except Exception: return return StringFromPython(python_equivalent) diff --git a/mathics/core/element.py b/mathics/core/element.py index 4d0848dd6..56603c7f5 100644 --- a/mathics/core/element.py +++ b/mathics/core/element.py @@ -422,7 +422,7 @@ def user_hash(self, update) -> None: # __hash__ might only hash a sample of the data available. raise NotImplementedError - def to_python(self, *args, python_form: bool = False, **kwargs): + def to_python(self, *args, **kwargs): # Returns a native builtin Python object # something in (int, float, complex, str, tuple, list or dict.). # (See discussions in @@ -430,12 +430,6 @@ def to_python(self, *args, python_form: bool = False, **kwargs): # and # https://github.com/Mathics3/mathics-core/pull/551 # - # - # if n_evaluation is an Evaluation object, then the expression - # is passed by an eval_N(). - # If python_form is True, the standard behaviour is changed, - # and it seems to behave like to_sympy.... - raise NotImplementedError def to_mpmath(self): diff --git a/mathics/core/expression.py b/mathics/core/expression.py index c3dae5233..df3f71fc6 100644 --- a/mathics/core/expression.py +++ b/mathics/core/expression.py @@ -1382,7 +1382,9 @@ def to_python(self, *args, **kwargs): """ from mathics.builtin.base import mathics_to_python - n_evaluation = kwargs.get("n_evaluation") + n_evaluation = kwargs.get("n_evaluation", None) + assert n_evaluation is None + head = self._head if head is SymbolFunction: @@ -1416,6 +1418,9 @@ def to_python(self, *args, **kwargs): # keywords=[], # ) return py_obj + + # Notice that in this case, `to_python` returns a Mathics Expression object, + # instead of a builtin native object. return self def to_sympy(self, **kwargs): diff --git a/mathics/core/symbols.py b/mathics/core/symbols.py index 717617e5f..aa17bb34c 100644 --- a/mathics/core/symbols.py +++ b/mathics/core/symbols.py @@ -596,22 +596,12 @@ def to_python(self, *args, python_form: bool = False, **kwargs): if value is not self: return value.to_python() - if python_form: - # TODO: consider to return self.value if available. - # For general symbols, one possibility would be to - # return a sympy symbol, also stored in value. - - # Also, why we need this parameter? If the idea is - # that to_python returns native (builtin) Python types, - # then to get a sympy object, we should use to_sympy. - return self.to_sympy(**kwargs) - else: - # For general symbols, the default behaviour is - # to return a 'str'. The reason seems to be - # that native (builtin) Python types - # are better for being used as keys in - # dictionaries. - return self.name + # For general symbols, the default behaviour is + # to return a 'str'. The reason seems to be + # that native (builtin) Python types + # are better for being used as keys in + # dictionaries. + return self.name def to_sympy(self, **kwargs): from mathics.builtin import mathics_to_sympy