type .__ getattribute__和object .__ getattribute__有什么区别? [英] What is the difference between type.__getattribute__ and object.__getattribute__?
问题描述
给出:
In [37]: class A:
....: f = 1
....:
In [38]: class B(A):
....: pass
....:
In [39]: getattr(B, 'f')
Out[39]: 1
好吧,这是调用super还是抓取mro?
Okay, that either calls super or crawls the mro?
In [40]: getattr(A, 'f')
Out[40]: 1
这是预期的.
In [41]: object.__getattribute__(A, 'f')
Out[41]: 1
In [42]: object.__getattribute__(B, 'f')
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-42-de76df798d1d> in <module>()
----> 1 object.__getattribute__(B, 'f')
AttributeError: 'type' object has no attribute 'f'
getattribute不执行getattr的工作是什么?
What is getattribute not doing that getattr does?
In [43]: type.__getattribute__(B, 'f')
Out[43]: 1
什么?! type.__getattribute__
调用超级,但object
的版本没有?
What?! type.__getattribute__
calls super but object
's version doesn't?
In [44]: type.__getattribute__(A, 'f')
Out[44]: 1
推荐答案
您直接在类上运行 . object.__getattribute__
仅在A
和B
的实例中使用.这是因为在类型上查找特殊方法;例如,类型是类.
You are operating directly on classes. object.__getattribute__
is only used on instances of A
and B
instead. That's because special methods are looked up on the type; for instances the type is the class.
那么对于类,类型是.. type
:
For classes then, the type is.. type
:
>>> class A:
... f = 1
...
>>> class B(A):
... pass
...
>>> type(B)
<class 'type'>
因此使用type.__getattribute__
:
>>> type.__getattribute__(B, 'f')
1
和object.__getattribute__
在实例上工作正常:
>>> object.__getattribute__(B(), 'f')
1
对于实例,首先在类上查找属性(对于数据描述符),然后在实例上,然后如果实例没有属性,则按MRO顺序搜索类层次结构.这是object.__getattribute__
的工作.因此object.__getattribute__
在属性和的第一个参数(例如self
,实例对象)中查看type(self).__mro__
中的对象.
For instances attributes are looked up first on the class (in the case of data descriptors), then on the instance, then if the instance doesn't have the attribute, the class hierarchy is searched in MRO order. This is the job of object.__getattribute__
. So object.__getattribute__
looks at the first argument (e.g. self
, the instance object) for the attribute, and at objects in type(self).__mro__
.
对于类,属性是在类本身及其所有基础上查找的; type.__getattribute__
直接在self.__mro__
中查找这些内容; self
在这里是一个类对象.
For classes, attributes are looked up on the class itself and all its bases; type.__getattribute__
looks directly at self.__mro__
for these; self
being a class object here.
如果对类使用object.__getattribute__
,则直接在B
上没有f
属性,在type(B).__mro__
中的任何地方都没有f
.如果使用type.__getattribute__
,则A
是B.__mro__
的成员,因此可以在其中找到f
:
If you use object.__getattribute__
for classes then, there is no f
attribute on B
directly, and no f
anywhere in type(B).__mro__
. If you use type.__getattribute__
, A
is a member of B.__mro__
so f
is found there:
>>> type(B).__mro__
(<class 'type'>, <class 'object'>)
>>> B.__mro__
(<class '__main__.B'>, <class '__main__.A'>, <class 'object'>)
这篇关于type .__ getattribute__和object .__ getattribute__有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!