Can you use -getattr- to get a function in the current module?
Alex Martelli
aleax at mac.com
Mon Sep 3 23:44:30 EDT 2007
Sergio Correia <sergio.correia at gmail.com> wrote:
> This works:
>
> # Module spam.py
>
> import eggs
>
> print getattr(eggs, 'omelet')(100)
>
> That is, I just call the function omelet inside the module eggs and
> evaulate it with the argument 100.
>
> But what if the function 'omelet' is in the module where I do the
> getattr (that is, in spam.py). If I do any of this
>
> print getattr(spam, 'omelet')(100)
> print getattr('','omelet')(100)
> print getattr('omelet')(100)
>
> It wont work. Any ideas?
globals() returns a dict of all globals defined so far, so, _after_ 'def
omelet ...' has executed, globals()['omelet'](100) should be OK.
Alex
More information about the Python-list
mailing list