理解“包装器描述符”本身的描述符协议 如果我要访问“包装器 - 描述符”类本身的对象,则试图探索描述符协议将如何工作。 因此,我在typeObject.c中探索了C代码,并找到了两个__get__

问题描述 投票:0回答:1
__get__

函数 -

slot_tp_descr_get

wrap_descr_get。它们与以下命令相关 -

TPSLOT(__get__, tp_descr_get, slot_tp_descr_get, wrap_descr_get,
           "__get__($self, instance, owner=None, /)\n--\n\nReturn an attribute of instance, which is of type owner.")
wrap_descr_get
是包装

slot_tp_descr_get
的包装函数。
通过

slot_tp_descr_get

的代码,我发现它称为
__get__
参数的
self

。因此,如果传递自己的类型的对象,它会无限地运行吗? IF语句前提的函数中有一个返回语句,但我认为它不会运行。因为,“如果子句”检查

self

是否实现了
__get__
的类,则由于我们通过了此类的一个对象,因此会找到
__get__
.
相关问题
如果我称其他类别(如函数类)为下面的其他类别。
__get__
是由

def fun(): pass type(fun).__get__ or fun.__get__
或按照描述符协议直接调用的函数类的基础

__get__

,它将寻找

wrap_descr_get

类(此处的包装器descriptor)并调用班级的
__get__

    

您把事情混在一起了。

“ wrapper_descriptor”类型,
__get__
是一种特定类型,其实例用于包装作为C插槽实现的方法。它与其他几种描述符类型一起实现。它的功能是
slot_tp_descr_get
,而不是您查看的任何功能。

types.WrapperDescriptorType
python cpython
1个回答
0
投票
Objects/descrobject.c

时提供C插槽实现的函数。这是像这样的班级的第阶段的函数:

tp_descr_get

	
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.