__getattr__ functionality for modules?
Alex
delete.this.part.alex.news at attbi.com
Sun May 18 18:11:25 EDT 2003
Stefan Franke wrote:
> Is there a way to achieve for modules what overriding __getattr__ does
> for classes?
>
> My problem: I want an infinite number of identifiers appear as if they
> were defined in the global namespace.
>
> The identifiers have structured names (say, id2m5, id2m6, id3m2, ...),
> and I'd like to generate the respective values on the fly and cache
> them for subsequent lookups.
>
> If they were attributes of an object it would be trivial, but how do I
> achieve this for name lookups in the __main__ (or any other) module?
>
> Stefan
My experience is that getattr works on modules other than __main__. For
__main__, I suppose you could use the globals() dictionary.
Alex
Python 2.2.2 (#1, May 11 2003, 11:57:38)
[GCC 3.2.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> getattr(os, 'path')
<module 'posixpath' from '/usr/lib/python2.2/posixpath.pyc'>
>>>
>>> class A:
... pass
...
>>> globals()['A']
<class __main__.A at 0x8125084>
>>> globals()['B']='this is a test'
>>> B
'this is a test'
>>>
More information about the Python-list
mailing list