Skip to main content
 首页 » 编程设计

python之为什么不将 `__get__` 转换为 `__get__.__get__`

2024年12月31日11findumars

来自 here我知道:每个函数实际上都是 class Function 的对象,并且由于描述符协议(protocol),像 a.b 这样的表达式将被传输到 b.__get__ (a,类型(a))。

但是这里的__get__还是一个对象(Function类的),那为什么b.__get__不会进一步转化为__get__.__get__(b,输入(b)) ?

请您参考如下方法:

描述符协议(protocol)没有内置在 Python 中,它内置在默认类型中。 实现(定义__get__, ...)和调用(调用member.__get__(self, type(self)) 该协议(protocol)分别由 function 和 object 类型以及其他类型完成。

Python 内部有裸函数和 getter,它们不实现 __get__, __set__, ... 方法并且不调用 member.__get__ 要么。 FunctionType.__get__ 就是这样一种情况:

>>> def method(self): 
...     return self 
... 
>>> method 
<function __main__.method(self)> 
>>> method.__get__ 
<method-wrapper '__get__' of function object at 0x10f4eb378> 
>>> method.__get__.__get__ 
AttributeError: 'method-wrapper' object has no attribute '__get__'