__getattr__ equivalent for a module
Maksim Kasimov
maksim.kasimov at gmail.com
Mon Jan 15 12:14:14 EST 2007
Hi Leif, many thanks - it works
Leif K-Brooks wrote:
> Maksim Kasimov wrote:
>> so my question is: how to tune up a module get default attribute if we
>> try to get access to not actually exists attribute of a module?
>
> You could wrap it in an object, but that's a bit of a hack.
>
> import sys
>
> class Foo(object):
> def __init__(self, wrapped):
> self.wrapped = wrapped
>
> def __getattr__(self, name):
> try:
> return getattr(self.wrapped, name)
> except AttributeError:
> return 'default'
>
> sys.modules[__name__] = Foo(sys.modules[__name__])
--
Maksim Kasimov
More information about the Python-list
mailing list