 DefineFunction - Maple Help

Home : Support : Online Help : Connectivity : Python : DefineFunction

Python

 DefineFunction
 parse and evaluate a Python def statement Calling Sequence DefineFunction(defn) Parameters

 defn - string Description

 • 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](..). Examples

 > $\mathrm{with}\left(\mathrm{Python}\right):$
 > $\mathrm{DefineFunction}\left("def mysum\left(a,b\right): return a+b"\right)$
 ${">"}$ (1)
 > $\mathrm{EvalString}\left("mysum\left(1,2\right)"\right)$
 ${3}$ (2)
 > $\mathrm{MySum}≔\mathrm{GetVariable}\left("mysum"\right)$
 ${\mathrm{MySum}}{≔}{">"}$ (3)
 > $\mathrm{MySum}\left(4,5\right)$
 ${9}$ (4)

Indentation matters; use \n for newlines and spaces or \t for tabs

 > $\mathrm{DefineFunction}\left("def flatten\left(lst\right): return sum\left( \left(\left[x\right] if not isinstance\left(x, list\right) else flatten\left(x\right) for x in lst\right), \left[\right] \right)"\right):$
 > $\mathrm{EvalString}\left("flatten\left(\left[\left[1\right],2,\left[\left[3\right],4,5\right]\right]\right)"\right)$
 $\left[{1}{,}{2}{,}{3}{,}{4}{,}{5}\right]$ (5) Compatibility

 • The Python[DefineFunction] command was introduced in Maple 2018.