Python metaclasses: Why isn't __setattr__ called for attributes set during class definition?
- 阅读更多 关于 Python metaclasses: Why isn't __setattr__ called for attributes set during class definition?
问题 I have the following python code: class FooMeta(type): def __setattr__(self, name, value): print name, value return super(FooMeta, self).__setattr__(name, value) class Foo(object): __metaclass__ = FooMeta FOO = 123 def a(self): pass I would have expected __setattr__ of the meta class being called for both FOO and a . However, it is not called at all. When I assign something to Foo.whatever after the class has been defined the method is called. What's the reason for this behaviour and is there