dynamic function call
sholden at holdenweb.com
Wed Dec 20 14:10:39 EST 2000
Hwanjo Yu <hwanjoyu at uiuc.edu> wrote in message
news:Ko606.141$G66.2664 at vixen.cso.uiuc.edu...
> Is there any way to bind a function name at runtime ?
> For instance, we don't know the function name to call, but we know the
> function name is saved in a variable, "fname".
> How to call it in this case ?
> Thanks in advance.
You just use the variable fname instead of the function name! For example:
>>> def myfun(x):
... print "x is", x
>>> yourfun = myfun
>>> yourfun('This is cool')
x is This is cool
The "def" statement simply binds the function name to its compiled code
body. If you "assign" the functin to another variable, the same call
mechanism can be used on that binding too.
More information about the Python-list