Python
DefineFunction
parse and evaluate a Python def statement
Calling Sequence
Parameters
Description
Examples
Compatibility
DefineFunction(defn)
defn
-
string
The DefineFunction command sends the given string, defn, to be executed by a Python interpreter. Typically the string defn will contain a Python function definition beginning with "def ".
This command is equivalent to calling Python:-EvalString with the output=none option.
This command always returns NULL. A subsequent EvalString or GetVariable command must be used if you want to get a reference to the procedure that was defined.
This function is part of the Python package, so it can be used in the short form DefineFunction(..) only after executing the command with(Python). However, it can always be accessed through the long form of the command by using Python[DefineFunction](..).
with⁡Python:
DefineFunction⁡def mysum(a,b): return a+b
<Python object: <function mysum at 0x7f38628c8720>>
EvalString⁡mysum(1,2)
3
MySum≔GetVariable⁡mysum
MySum≔<Python object: <function mysum at 0x7f38628c8720>>
MySum⁡4,5
9
Indentation matters; use \n for newlines and spaces or \t for tabs
DefineFunction⁡def flatten(lst):\n\treturn sum( ([x] if not isinstance(x, list) else flatten(x)\n\tfor x in lst), [] ):
EvalString⁡flatten([[1],2,[[3],4,5]])
1,2,3,4,5
The Python[DefineFunction] command was introduced in Maple 2018.
For more information on Maple 2018 changes, see Updates in Maple 2018.
See Also
Python:-EvalString
Python:-EvalFunction
Download Help Document