__getattr__ equivalent for a module

Discussion in 'Python' started by Maksim Kasimov, Jan 15, 2007.

  1. Hi,

    in any python class it is possible to define __getattr__ method so that if we try to get some value of not actually exists instance attribute, we can get some default value.

    For example:

    class MyClass:

    def __getattr__(self, attname):

    if attname.startswith('a'):
    return "*"


    i = MyClass()
    ....
    i.aValue # it gives "*" if "i.aValue" will not be set before this call


    i need to define the same behavior for a module:

    import mymodule
    mymodule.anyattribute
    or
    from mymodule import anyattribute

    "anyattribute" is not actually defined in the module, but gives some attribute of the module

    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?

    (python 2.4 or 2.2)

    many thanks for help.


    --
    Maksim Kasimov
     
    Maksim Kasimov, Jan 15, 2007
    #1
    1. Advertising

  2. 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__])
     
    Leif K-Brooks, Jan 15, 2007
    #2
    1. Advertising

  3. 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
     
    Maksim Kasimov, Jan 15, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Greg Brunet

    __getattr__ weirdness

    Greg Brunet, Aug 22, 2003, in forum: Python
    Replies:
    3
    Views:
    339
    Greg Brunet
    Aug 22, 2003
  2. Christian Hudon

    __getattr__-like method for current module.

    Christian Hudon, Aug 16, 2004, in forum: Python
    Replies:
    0
    Views:
    338
    Christian Hudon
    Aug 16, 2004
  3. aum
    Replies:
    1
    Views:
    135
  4. Replies:
    0
    Views:
    137
  5. Jean-Michel Pichavant
    Replies:
    0
    Views:
    135
    Jean-Michel Pichavant
    Sep 18, 2012
Loading...

Share This Page