How to pass a reference to the current module

Steven D'Aprano steve at REMOVE.THIS.cybersource.com.au
Fri Aug 3 21:52:30 EDT 2007


On Fri, 03 Aug 2007 18:32:54 -0700, James Stroud wrote:

> Carsten Haese wrote:
>> This seems to confirm my suspicion that the do_something_with function
>> doesn't actually need a reference to the module, it only needs a
>> reference to the function to call.
>> 
>> Maybe your hurdle is how to obtain a reference to a function from the
>> current module when all you have is the name of the function in a
>> string. The answer to that would be "globals()[funcname]".
> 
> This is like a bad case of phone-tag. Please see my response to your 
> previous post for why this does not seem feasible to me.


I've read your previous post, and I don't understand why you consider it
unfeasible. The user provides a function name in a config file, which
gives you funcname as a string. You already know which module it comes
from (you didn't specify how, but all your examples show that). Carsten is
simply showing you how to get the function from the function name without
the nasty hack using sys._getframe. Why is in unfeasible?


-- 
Steven.




More information about the Python-list mailing list