Can Python do this?

Henrik Motakef henrik.motakef at web.de
Mon Mar 4 11:37:13 EST 2002


"Robert Oschler" <Oschler at earthlink.net> writes:

> Can I then call that method by somehow having the interpreter
> evaluate the string variable into a call to the desired method?

You can. It's (unsurprisingly) called "eval".

>>> def func1(): print "foo"
... 
>>> eval("func" + "1")
<function func1 at 0x80e3bcc>
>>> eval("func" + "1()")
foo
>>> eval("func" + "1")()
foo

hth
Henrik



More information about the Python-list mailing list