Python
EvalString
parse and evaluate a Python statement
Calling Sequence
Parameters
Options
Description
Examples
Compatibility
EvalString(cmd)
EvalString(cmd,opts)
cmd
-
string
opts
(optional) zero or more options as specified below
output = one of maple,python, or none
Specifies the output the evaluation should produce.
By default the value returned is converted into a Maple data structure. If no conversion is available, the return value will be an expression of type python, a Python object reference that can be used in subsequent calls to Python.
To avoid Maple conversions of the return value and always get a Python object, use the option, output=python.
To avoid all conversions, and omit the return value, use the option, output=none.
The EvalString command sends the given string to be executed by a Python interpreter.
By default the given cmd is prepended with syntax to form a temporary variable assignment so that the result can be fetched and sent back to Maple as a reference or converted data structure. This is the default output=maple mode.
To avoid data-structure conversions use output=python. With this option, even trivial conversions like integer results will remain as Python object references. More typically this output mode is used for very large results that need to be passed to another Python function, but will not otherwise be used directly in Maple.
The output=none option is required for statements where it is syntactically invalid to be prefixed with a temporary variable assignment. This includes procedure declarations, import statements, and multi-line scripts among other things.
Evaluating Python commands is stateful. Variables assigned in previous commands remain assigned and can be used in subsequent commands until a Maple restart is executed.
This function is part of the Python package, so it can be used in the short form EvalString(..) only after executing the command with(Python). However, it can always be accessed through the long form of the command by using Python[EvalString](..).
with⁡Python:
A simple evaluation:
EvalString⁡1+1
2
Remember ^ means bitwise XOR in Python:
EvalString⁡2^10
8
Access something built in
ImportModule⁡statistics
EvalString⁡statistics.median_grouped([1, 3, 3, 5, 7], interval=2)
3.50000000000000
When defining a procedure make sure you use the output=none option
EvalString⁡def mysum(a,b): return a+b,output=none
EvalString⁡mysum(1,2)
3
Indentation matters; use \n for newlines and spaces or \t for tabs
EvalString⁡def flatten(lst):\n\treturn sum( ([x] if not isinstance(x, list) else flatten(x)\n\tfor x in lst), [] ),output=none:
EvalString⁡flatten([[1],2,[[3],4,5]])
1,2,3,4,5
Multiple statements can be evaluated at the same time, but the result returned to Maple is from the FIRST statement, not the last.
EvalString⁡a=1\nb=2\na+b
1
It is recommended to use output=none when evaluating multiple statements at the same time
EvalString⁡a=1\nb=2\nr=a+b,output=none
EvalString⁡r
GetVariable⁡r
To avoid automatic data-structure conversion use output=python
emptystring≔EvalString⁡'',output=python
emptystring≔<Python object: >
emptystring:-join⁡a,b,c
abc
The Python[EvalString] command was introduced in Maple 2018.
For more information on Maple 2018 changes, see Updates in Maple 2018.
See Also
Python:-EvalFunction
type/python
Download Help Document