[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/19/2008 5:34:00 AM

Gabriel Genellina wrote:
> En Mon, 18 Feb 2008 14:49:02 -0200, Alex <noname9968@gmail.com> escribió:
>> 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?
> I don't understand the question (even with the later correction
> namespaces->objects)
There's no question anymore, I just failed to distinguish function local
variables (which don't include __name__) and function object's attributes
>>> 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.
>>
>
> The function could receive a namespace to work with (a dictionary). Then
> you just call it with globals() == the namespace of the calling module.
Yes, but access to module seems more verbose:

>>> module_dict['x']()
xxx

Instead of just:

>>> module.x()
xxx
1 Answer

Gabriel Genellina

2/19/2008 11:06:00 AM

0

On 19 feb, 03:33, Alex <noname9...@gmail.com> wrote:
> GabrielGenellinawrote:
> > En Mon, 18 Feb 2008 14:49:02 -0200, Alex <noname9...@gmail.com> escribió:
> >> 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?
> > I don't understand the question (even with the later correction  
> > namespaces->objects)
>
> There's no question anymore, I just failed to distinguish function local
> variables (which don't include __name__) and function object's attributes>>> 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.
>
> > The function could receive a namespace to work with (a dictionary). Then  
> > you just call it with globals() == the namespace of the calling module.
>
> Yes, but access to module seems more verbose:
>
>  >>> module_dict['x']()
> xxx
>
> Instead of just:
>
>  >>> module.x()
> xxx

You could write a wrapper class on the client side, but I guess it's
easier to pass the module object directly, as you said earlier.
Anyway, this class would fake attribute access for dictionary entries:

py> class Idx2Attr(object):
... def __init__(self, d): self.__dict__[None] = d
... def __getattr__(self, name):
... try: return self.__dict__[None][name]
... except KeyError: raise NameError, name
... def __setattr__(self, name, value):
... self.__dict__[None][name]=value
...
py> import htmllib
py> dir(htmllib)
['AS_IS', 'HTMLParseError', 'HTMLParser', '__all__', '__buil
tins__', '__doc__', '__file__', '__name__', 'sgmllib', 'test
']
py> mod = Idx2Attr(htmllib.__dict__) # htmllib.__dict__ is what you
get if you use globals() inside htmllib
py> mod.__file__
'C:\\APPS\\PYTHON25\\lib\\htmllib.pyc'
py> mod.foo = 3
py> htmllib.foo
3

([None] is to avoid name collisions to some extent)

--
Gabriel Genellina