[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Re: How to get current module object

Unnamed One

2/18/2008 4:49:00 PM

Gabriel Genellina wrote:
> En Sun, 17 Feb 2008 16:25:44 -0200, Alex <noname9968@gmail.com> escribi�:
>
>> Can I get reference to module object of current module (from which the
>> code is currently executed)? I know __import__('filename') should
>> probably do that, but the call contains redundant information (filename,
>> which needs to be updated), and it'll perform unnecessary search in
>> loaded modules list.
>>
>> It shouldn't be a real problem (filename can probably be extracted from
>> the traceback anyway), but I wonder if there is more direct and less
>> verbose way.
>>
> sys.modules[__name__]
>
That's what I've been searching for, thanks. By the way, I know it might
be trivial question... but function and class namespaces have __name__
attribute too. Why is global one always returned?
> Why do you want to get the module object? globals() returns the module
> namespace, its __dict__, perhaps its only useful attribute...
To pass it as a parameter to a function (in another module), so it can
work with several modules ("plugins" for main program) in a similar manner.