Getting a function name from string

Paul McGuire ptmcg at austin.rr._bogus_.com
Wed Nov 2 18:07:06 EST 2005


"David Rasmussen" <david.rasmussen at gmx.net> wrote in message
news:436943e0$0$2083$edfadb0f at dtext02.news.tele.dk...
> If I have a string that contains the name of a function, can I call it?
> As in:
>
> def someFunction():
> print "Hello"
>
> s = "someFunction"
> s() # I know this is wrong, but you get the idea...
>
> /David

Lookup the function in the vars() dictionary.

>>> def fn(x):
...   return x*x
...
>>> vars()['fn']
<function fn at 0x009D67B0>
>>> vars()['fn'](100)
10000

-- Paul





More information about the Python-list mailing list