Dynamic function calling

Olaf Meyer olaf.meyer at nokia.com
Fri Sep 1 14:08:25 EDT 2000


Thanks to all of you for the suggestions! I do have anoter small question:
Is it possible to determine the name of the currently executing function?

Olaf

Burkhard Kloss wrote:

> > I'm wondering if it is possible to call functions just by having access
> > to their name in a string value. Something similar to:
> >
> >   def test():
> >      print "test function"
> >
> >   f = "test"
> >   apply(f, ())
> >
> > This does not work of course, because f does not have the right type.
> > Is there a way to get a function object from the name (string)?
>
> If you are in the same module as the function you can call through locals:
>
> locals() ['test'] ()
>
> or, breaking down the steps
>
> mydict = locals()
> myfunc = mydict ['test']
> myfunc ()
>
> If it's in another module, say, foo, you just say:
>
> foo.__dict__ ['test'] ()
>
> HTH,
>
>     Burkhard




More information about the Python-list mailing list