問(wèn)題描述
根據(jù)本指南關(guān)于 python 描述符https://docs.python.org/howto/descriptor.html
according to this guide on python descriptors https://docs.python.org/howto/descriptor.html
新樣式類中的方法對(duì)象是使用描述符實(shí)現(xiàn)的,以避免在屬性查找中對(duì)它們進(jìn)行特殊封裝.
method objects in new style classes are implemented using descriptors in order to avoid special casing them in attribute lookup.
我理解的方式是有一個(gè)方法對(duì)象類型實(shí)現(xiàn)了 __get__
并在使用實(shí)例調(diào)用時(shí)返回綁定的方法對(duì)象,在沒(méi)有實(shí)例且僅調(diào)用時(shí)返回未綁定的方法對(duì)象班級(jí).文章還指出,這個(gè)邏輯是在 object.__getattribute__
方法中實(shí)現(xiàn)的.像這樣:
the way I understand this is that there is a method object type that implements __get__
and returns a bound method object when called with an instance and an unbound method object when called with no instance and only a class. the article also states that this logic is implemented in the object.__getattribute__
method. like so:
def __getattribute__(self, key):
"Emulate type_getattro() in Objects/typeobject.c"
v = object.__getattribute__(self, key)
if hasattr(v, '__get__'):
return v.__get__(None, self)
return v
然而 object.__getattribute__
本身就是一個(gè)方法!那么它如何綁定到一個(gè)對(duì)象(沒(méi)有無(wú)限遞歸)?如果它在屬性查找中是特殊大小寫的,這不會(huì)破壞刪除舊樣式特殊大小寫的目的嗎?
however object.__getattribute__
is itself a method! so how is it bound to an object (without infinite recursion)? if it is special cased in the attribute lookup does that not defeat the purpose of removing the old style special casing?
推薦答案
實(shí)際上,在 CPython 中默認(rèn)的 __getattribute__
實(shí)現(xiàn)不是 Python 方法,而是在C. 它可以直接訪問(wèn)對(duì)象槽(代表 Python 對(duì)象的 C 結(jié)構(gòu)中的條目),而無(wú)需通過(guò)討厭的屬性訪問(wèn)例程.
Actually, in CPython the default __getattribute__
implementation is not a Python method, but is instead implemented in C. It can access object slots (entries in the C structure representing Python objects) directly, without bothering to go through the pesky attribute access routine.
僅僅因?yàn)槟?Python 代碼必須這樣做,并不意味著 C 代碼必須這樣做.:-)
Just because your Python code has to do this, doesn't mean the C code has to. :-)
如果你確實(shí)實(shí)現(xiàn)了 Python __getattribute__
方法,只需使用 object.__getattribute__(self, attrname)
,或者更好的是,super().__getattribute__(attrname)
以訪問(wèn) self
上的屬性.這樣你也不會(huì)遇到遞歸.
If you do implement a Python __getattribute__
method, just use object.__getattribute__(self, attrname)
, or better still, super().__getattribute__(attrname)
to access attributes on self
. That way you won't hit recursion either.
在CPython實(shí)現(xiàn)中,屬性訪問(wèn)實(shí)際上是由tp_getattro
槽 在 C 類型對(duì)象中,回退到 tp_getattr
slot.
In the CPython implementation, the attribute access is actually handled by the tp_getattro
slot in the C type object, with a fallback to the tp_getattr
slot.
為了詳盡并完全公開(kāi) C 代碼的功能,當(dāng)您對(duì) 實(shí)例 使用屬性訪問(wèn)時(shí),以下是調(diào)用的完整函數(shù)集:
To be exhaustive and to fully expose what the C code does, when you use attribute access on an instance, here is the full set of functions called:
Python 將屬性訪問(wèn)轉(zhuǎn)換為對(duì) <代碼>PyObject_GetAttr() C 函數(shù).該函數(shù)的實(shí)現(xiàn)查找
tp_getattro
或tp_getattr
插槽.
Python translates attribute access to a call to the
PyObject_GetAttr()
C function. The implementation for that function looks up thetp_getattro
ortp_getattr
slot for your class.
object
類型具有 用 tp_getattro 槽-L1336" rel="nofollow noreferrer">PyObject_GenericGetAttr
函數(shù),它將調(diào)用委托給 _PyObject_GenericGetAttrWithDict
(*dict
指針設(shè)置為 NULL
和 suppress
參數(shù)設(shè)置為 0
).這個(gè)函數(shù)是你的 object.__getattribute__
方法(一個(gè) 特殊表名稱和插槽之間的映射).
The object
type has filled the tp_getattro
slot with the PyObject_GenericGetAttr
function, which delegates the call to _PyObject_GenericGetAttrWithDict
(with the *dict
pointer set to NULL
and the suppress
argument set to 0
). This function is your object.__getattribute__
method (a special table maps between the name and the slots).
這個(gè)_PyObject_GenericGetAttrWithDict
函數(shù)可以通過(guò)__dict__對(duì)象typeobj.html#c.PyTypeObject.tp_dict" rel="nofollow noreferrer">tp_dict
slot,但對(duì)于 descriptors(包括方法),_PyType_Lookup
函數(shù).
This _PyObject_GenericGetAttrWithDict
function can access the instance __dict__
object through the tp_dict
slot, but for descriptors (including methods), the _PyType_Lookup
function is used.
_PyType_Lookup
處理緩存并委托給 find_name_in_mro
緩存未命中;后者查找類(和超類)的屬性.該代碼使用指向 MRO 中每個(gè)類上的 tp_dict
槽的直接指針來(lái)引用類屬性.
_PyType_Lookup
handles caching and delegates to find_name_in_mro
on cache misses; the latter looks up attributes on the class (and superclasses). The code uses direct pointers to the tp_dict
slot on each class in the MRO to reference class attributes.
如果 _PyType_Lookup
找到一個(gè)描述符,它會(huì)返回到 _PyObject_GenericGetAttrWithDict
并調(diào)用該對(duì)象上的 tp_descr_get
函數(shù)(__get__
鉤子).
If a descriptor is found by _PyType_Lookup
it is returned to _PyObject_GenericGetAttrWithDict
and it calls the tp_descr_get
function on that object (the __get__
hook).
當(dāng)您訪問(wèn) 類本身 上的屬性時(shí),而不是 _PyObject_GenericGetAttrWithDict
,type->tp_getattro
插槽由type_getattro()
函數(shù),它也考慮了元類.此版本也調(diào)用 __get__
,但將實(shí)例參數(shù)設(shè)置為 None
.
When you access an attribute on the class itself, instead of _PyObject_GenericGetAttrWithDict
, the type->tp_getattro
slot is instead serviced by the type_getattro()
function, which takes metaclasses into account too. This version calls __get__
too, but leaves the instance parameter set to None
.
此代碼無(wú)需遞歸調(diào)用 __getattribute__
即可訪問(wèn) __dict__
屬性,因?yàn)樗梢灾苯釉L問(wèn) C 結(jié)構(gòu).
Nowhere does this code have to recursively call __getattribute__
to access the __dict__
attribute, as it can simply reach into the C structures directly.
這篇關(guān)于__getattribute__ 方法和描述符的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!