尽管var1
是ChildClass
类的成员,但为什么我不能使用var1
访问ChildClass.var1
class MyType(type):
def __getattribute__(self, name):
print('attr lookup for %s' % str(name))
return object.__getattribute__(self, name)
class BaseClass(object):
__metaclass__ = MyType
var1 = 5
class ChildClass(BaseClass):
var2 = 6
print(ChildClass.var2) #works
print(ChildClass.var1) #fails
我遇到以下错误
AttributeError: 'MyType' object has no attribute 'var1'
谢谢
答案 0 :(得分:2)
由于MyType
是type
,请使用type.__getattribute__
而不是object.__getattribute__
:
class MyType(type):
def __getattribute__(self, name):
print('attr lookup for %s' % str(name))
return type.__getattribute__(self, name)