Python: Metaclass properties override class attributes, sometimes?

元气小坏坏 提交于 2019-12-10 14:34:50

问题


The result of the below code boggles me:

class MyClass(type):
    @property
    def a(self):
        return 1

class MyObject(object):
    __metaclass__ = MyClass

    a = 2

print MyObject.a
print object.__getattribute__(MyObject, 'a')
print type.__getattribute__(MyObject, 'a')
print MyObject.__dict__['a']
print MyObject().a

I really expect this to just print 2 repeatedly, but it prints 1 1 1 2 2. Is there a way this makes any intuitive sense?


To clarify: I understand that this behavior is well documented (here, "data descriptors"), but I want to have an understanding of why this makes sense, and why the core devs implemented descriptors this way.


回答1:


Properties are data descriptors; in attribute lookups, they take priority over identically-named entries in the dict of an instance of the class with the property. That means that with

MyObject.a

the a property in MyClass takes priority over the a entry in MyObject's dict. Similarly,

object.__getattribute__(MyObject, 'a')
type.__getattribute__(MyObject, 'a')

object.__getattribute__ and type.__getattribute__ both respect the priority of data descriptors over instance dict entries, so the property wins.

On the other hand,

MyObject.__dict__['a']

this explicitly does a dict lookup. It only sees things in MyObject's dict, ignoring the normal attribute lookup mechanisms.

For the last line:

MyObject().a

the MyClass descriptor only applies to instances of MyClass, not instances of its instances. The attribute lookup mechanism doesn't see the property.




回答2:


Here is some very similar code that might help you understand what's happening

class MyClass(object):
    def __init__(self, data):
        self.__dict__.update(data) # this would fail if I had self.a = a

    @property
    def a(self):
        return 1

MyObject = MyClass({'a': 2})

print MyObject.a
print object.__getattribute__(MyObject, 'a')
print MyObject.__dict__['a']

What you're seeing is that the instance (the class) having both a descriptor (your property) on its class and an attribute of the same name. Normally, there are safeguards to protect this, but the way type works goes around them.

So you have

 print MyObject.a

The descriptor beats the __dict__ entry and the property is called. This is because of the implementation of object.__getattribute__, at least conceptually.

 print object.__getattribute__(MyObject, 'a')

This is the same thing as saying MyObject.a, except if object.__getattribute__ was overridden. __getattribute__ is where the behavior of trying descriptors first comes from.

 print type.__getattribute__(MyObject, 'a')

This is the same thing as object.__getattribute__ because type doesn't override __getattribute__.

 print MyObject.__dict__['a']

This looks it up in your __dict__, which is the only place you stored 2. This is just a dict object (maybe/almost), so it's not going to look stuff up anywhere else.

 print MyObject().a

The way that attribute works, you're not accessing your type's attributes the same way you would directly. This is probably the part that doesn't have a super-intuitive answer



来源:https://stackoverflow.com/questions/22586327/python-metaclass-properties-override-class-attributes-sometimes

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!