static attributes & __getattr__ override

Vadim Suvorov vsuvorov at cccglobal.com
Wed Apr 12 13:15:48 EDT 2000


Hello,

I am trying to redefine class attributes access, to include static
attributes definitions from class (shared by all class instances). It is ok
for instance to shade class attribute. So I wrote

class MyClass:
    var1 = 1
    var2 = 2
    def __init__(self, x3):
        self.var3 = x3

    def __getattr__(self, name):
        return self.__class__.__dict__.get(name, None)

MyObj = MyClass(3)
print MyObj.var1, MyObj.var2, MyObj.var3, MyObj.var4

(expected result) >>> 1 2 3 None

It works fine for attributes. However, I can not access standard methods of
MyClass. I resorted to define __str__ and __repr__ (to be able to 'print
MyObj'), and even then I could not use MyObj <> None which really hurts me.

Can you advice me on best way to proceed, and explain briefly what is going
on? I do not think it is a bug, I probably missing something in the way
attributes/methods work.

Thanks,

Vadim





More information about the Python-list mailing list