久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

<small id='7n6KG'></small><noframes id='7n6KG'>

      <bdo id='7n6KG'></bdo><ul id='7n6KG'></ul>
  • <tfoot id='7n6KG'></tfoot>
  • <legend id='7n6KG'><style id='7n6KG'><dir id='7n6KG'><q id='7n6KG'></q></dir></style></legend>
      <i id='7n6KG'><tr id='7n6KG'><dt id='7n6KG'><q id='7n6KG'><span id='7n6KG'><b id='7n6KG'><form id='7n6KG'><ins id='7n6KG'></ins><ul id='7n6KG'></ul><sub id='7n6KG'></sub></form><legend id='7n6KG'></legend><bdo id='7n6KG'><pre id='7n6KG'><center id='7n6KG'></center></pre></bdo></b><th id='7n6KG'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='7n6KG'><tfoot id='7n6KG'></tfoot><dl id='7n6KG'><fieldset id='7n6KG'></fieldset></dl></div>

        __getattribute__ 方法和描述符

        The __getattribute__ method and descriptors(__getattribute__ 方法和描述符)
          <tbody id='LR2xT'></tbody>

          <i id='LR2xT'><tr id='LR2xT'><dt id='LR2xT'><q id='LR2xT'><span id='LR2xT'><b id='LR2xT'><form id='LR2xT'><ins id='LR2xT'></ins><ul id='LR2xT'></ul><sub id='LR2xT'></sub></form><legend id='LR2xT'></legend><bdo id='LR2xT'><pre id='LR2xT'><center id='LR2xT'></center></pre></bdo></b><th id='LR2xT'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='LR2xT'><tfoot id='LR2xT'></tfoot><dl id='LR2xT'><fieldset id='LR2xT'></fieldset></dl></div>
                <bdo id='LR2xT'></bdo><ul id='LR2xT'></ul>
                <tfoot id='LR2xT'></tfoot>
                1. <small id='LR2xT'></small><noframes id='LR2xT'>

                  <legend id='LR2xT'><style id='LR2xT'><dir id='LR2xT'><q id='LR2xT'></q></dir></style></legend>

                  本文介紹了__getattribute__ 方法和描述符的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

                  問(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_getattrotp_getattr 插槽.

                  • Python translates attribute access to a call to the PyObject_GetAttr() C function. The implementation for that function looks up the tp_getattro or tp_getattr slot for your class.

                  object 類型具有 用 tp_getattro 槽-L1336" rel="nofollow noreferrer">PyObject_GenericGetAttr 函數(shù),它將調(diào)用委托給 _PyObject_GenericGetAttrWithDict(*dict 指針設(shè)置為 NULLsuppress 參數(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_GenericGetAttrWithDicttype->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)!

                  【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

                  相關(guān)文檔推薦

                  python: Two modules and classes with the same name under different packages(python:不同包下同名的兩個(gè)模塊和類)
                  Configuring Python to use additional locations for site-packages(配置 Python 以使用站點(diǎn)包的其他位置)
                  How to structure python packages without repeating top level name for import(如何在不重復(fù)導(dǎo)入頂級(jí)名稱的情況下構(gòu)造python包)
                  Install python packages on OpenShift(在 OpenShift 上安裝 python 包)
                  How to refresh sys.path?(如何刷新 sys.path?)
                  Distribute a Python package with a compiled dynamic shared library(分發(fā)帶有已編譯動(dòng)態(tài)共享庫(kù)的 Python 包)
                  <i id='5qOsR'><tr id='5qOsR'><dt id='5qOsR'><q id='5qOsR'><span id='5qOsR'><b id='5qOsR'><form id='5qOsR'><ins id='5qOsR'></ins><ul id='5qOsR'></ul><sub id='5qOsR'></sub></form><legend id='5qOsR'></legend><bdo id='5qOsR'><pre id='5qOsR'><center id='5qOsR'></center></pre></bdo></b><th id='5qOsR'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='5qOsR'><tfoot id='5qOsR'></tfoot><dl id='5qOsR'><fieldset id='5qOsR'></fieldset></dl></div>
                    <bdo id='5qOsR'></bdo><ul id='5qOsR'></ul>

                  • <small id='5qOsR'></small><noframes id='5qOsR'>

                    <legend id='5qOsR'><style id='5qOsR'><dir id='5qOsR'><q id='5qOsR'></q></dir></style></legend>
                      <tbody id='5qOsR'></tbody>

                      1. <tfoot id='5qOsR'></tfoot>
                            主站蜘蛛池模板: 日本又色又爽又黄又高潮 | 婷婷色国产偷v国产偷v小说 | 亚洲 欧美 综合 | 精品成人佐山爱一区二区 | 亚洲自拍一区在线观看 | 九色网址| 91久久久久久久久久久 | 91免费观看国产 | 国产美女精品 | 麻豆精品一区二区三区在线观看 | 天天射色综合 | 精品国产18久久久久久二百 | 日韩成人影院 | 夫妻午夜影院 | 久在线视频播放免费视频 | 日韩a在线| 天天影视综合 | 欧美一区中文字幕 | 日韩国产一区二区三区 | 久久丝袜视频 | 欧美一区 | www.国产日本 | 久久精品亚洲 | 成人在线视频免费观看 | 欧美国产日韩在线观看 | 99re在线播放 | 亚洲精品91 | 欧美在线观看免费观看视频 | av免费观看网站 | 日韩不卡在线 | 欧美久久久久久久久 | 久久99精品国产99久久6男男 | 午夜精品一区 | 欧州一区二区三区 | 中文字幕在线观看一区 | 国产精品视频一区二区三 | 亚洲美女网站 | 伊人久久综合 | 国产精久久久久久久妇剪断 | av日韩精品| 国产观看|