英文:
Invocation of Python's descriptor protocol from super
问题
在Python的描述符指南中写道:
> 像super(A, obj).m
这样的点查找会在obj.__class__.__mro__
中搜索A
之后紧接着的基类B
,然后返回B.__dict__['m'].__get__(obj, A)
。如果不是描述符,将返回m
未更改的。
这似乎暗示属性m
的搜索仅发生在类B
的__dict__
中,而类B
是紧随A
之后的基类,但我认为它应该在A
之后的MRO中的所有基类中进行。我有遗漏什么吗?
英文:
In the Python Descriptor HowTo guide, it is written that:
> A dotted lookup such as super(A, obj).m
searches obj.__class__.__mro__
> for the base class B
immediately following A
and then returns
> B.__dict__['m'].__get__(obj, A)
. If not a descriptor, m
is returned
> unchanged.
This seems to suggest that the search for attribute m
only happens in the __dict__
of class B
, which is the base class immediately following A
, however, I think it should happen in all of the base classes in the mro following A. Am I missing anything here?
答案1
得分: 1
查找基本上是通常的算法,但省略了一些开始的步骤:
- 在
type(m)
的MRO中查找m
,从A
之后的类开始。
一旦找到了m
,然后Python会检查m
是否是描述符。如果是,将调用m.__get__
,其返回值将是结果。否则,m
本身就是结果。
英文:
The lookup is basically the usual algorithm, but with a couple of beginning steps skipped:
- <strike>Look for
m
inobj.__dict__
.</strike> - <strike>Look for
m
intype(m).__dict__
.</strike> - Look for
m
in a class in the MRO oftype(m)
, starting with the class afterA
.
Once m
is found, then Python checks if m
is a descriptor. If it is, m.__get__
is called and its return value is the result. Otherwise, m
itself is the result.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论