[Beginner] Calling a function by its name in a string
Grant Edwards
grante at visi.com
Wed Jul 27 14:38:35 EDT 2005
On 2005-07-27, Paolino <paolo.veronelli at gmail.com> wrote:
>> Is there a metalanguage capability in Python (I know there are many) to
>> call a function having its name in a string?
> eval('foo()') should do, but it's said a bad practice ;)
An alternative to eval() is:
>>> def foo():
... print "foo was called"
...
>>> s = "foo"
>>> globals()[s]()
foo was called
>>>
--
Grant Edwards grante Yow! I'm meditating on
at the FORMALDEHYDE and the
visi.com ASBESTOS leaking into my
PERSONAL SPACE!!
More information about the Python-list
mailing list