Reference current module?

Tobiah toby at tobiah.org
Wed Jun 20 15:03:08 EDT 2007


Gabriel Genellina wrote:
> En Tue, 19 Jun 2007 16:34:50 -0300, Steven Bethard 
> <steven.bethard at gmail.com> escribió:
> 
>> Tobiah wrote:
>>>
>>> getattr(current_module, 'foo')
>>>
>>> where 'current_module' is a handle the the one
>>> that the code is in?  Just like

> A simple way would be using sys.modules[__name__].
> But I prefer using globals()['foo'] as it is simpler and does not assume 
> additional requirements (like __name__ still being the same, or the 
> module still available for importing).
> 
> --Gabriel Genellina
> 

In this case I really wanted a reference to
the actual module, so that I could walk down
from there in a sort of object mapping:

def remote_call(call = 'foo.bar.other.thing'):
	destination_object = sys.modules[__name__]
	for part in call.split('.'):
		destination_object = getattr(destination_object, part)

Tobiah

-- 
Posted via a free Usenet account from http://www.teranews.com




More information about the Python-list mailing list