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

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

        <small id='gvydb'></small><noframes id='gvydb'>

        “私人"名稱修改和實例與類屬性

        quot;Privatequot; name mangling and instance vs class attributes(“私人名稱修改和實例與類屬性)
        <legend id='kmIBY'><style id='kmIBY'><dir id='kmIBY'><q id='kmIBY'></q></dir></style></legend>

        • <tfoot id='kmIBY'></tfoot>

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

            • <small id='kmIBY'></small><noframes id='kmIBY'>

                    <tbody id='kmIBY'></tbody>
                • 本文介紹了“私人"名稱修改和實例與類屬性的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  我正在編寫一個需要訪問私有變量的裝飾器并發現了這種差異.誰能解釋一下?

                  I was writing a decorator that needs to access private variables and found this discrepancy. Can anyone explain this?

                  (Python 2.5)

                  (Python 2.5)

                  命名修改對類中定義的屬性按預期工作:

                  Naming mangling works as expected for attributes defined in the class:

                  >>> class Tester(object):
                  ...    __foo = "hi"
                  
                  >>> t = Tester()
                  >>> t._Tester__foo
                  'hi'
                  

                  實例屬性不起作用(這是我們應該做的正確的方式?)

                  Instance attributes do not work (and this is the way we are supposed to do it right?)

                  >>> class Tester(object):
                  ...     def __init__(self):
                  ...         self.__foo = "hi"
                  
                  >>> t = Tester()
                  >>> t._Tester__foo
                  AttributeError: 'Tester' object has no attribute '_Tester__foo'
                  

                  附:類屬性"是正確的詞嗎?它們不是靜態的,但如果您將其中之一設為列表或其他可變類型,則它是共享的...

                  P.S. Is "class attribute" the right word for these? They aren't static, but if you make one of those a list, or some other mutable type, it is shared...

                  更新

                  事實上,第二個例子也很好用.這是硬件問題(重啟有幫助).

                  In fact, second example works fine, too. It was a hardware issue (restart helped).

                  推薦答案

                  這其實是正確的.

                  名稱修改發生在類創建時;任何引用重整名稱的函數也會被調整.

                  Name mangling takes place at class creation time; any functions that refer to mangled names are adjusted as well.

                  我無法重現您的示例,至少不能在 Mac 上的 Python 版本 2.4、2.5、2.6、3.1 和 3.2 中重現:

                  I cannot reproduce your example, at least not in Python versions 2.4, 2.5, 2.6, 3.1 and 3.2 on the Mac:

                  >>> class Tester(object):
                  ...     def __init__(self):
                  ...         self.__foo = "hi"
                  ... 
                  >>> Tester()._Tester__foo
                  'hi'
                  >>> Tester().__foo
                  Traceback (most recent call last):
                    File "<stdin>", line 1, in <module>
                  AttributeError: 'Tester' object has no attribute '__foo'
                  

                  如果你反匯編函數字節碼,你可以看到名字也被破壞了:

                  If you disassemble the function bytecode you can see the name has been mangled as well:

                  >>> import dis
                  >>> dis.dis(Tester.__init__)
                    3           0 LOAD_CONST               1 ('hi')
                                3 LOAD_FAST                0 (self)
                                6 STORE_ATTR               1 (_Tester__foo)
                                9 LOAD_CONST               0 (None)
                               12 RETURN_VALUE        
                  

                  我檢查了 編譯器源代碼 和 所有名稱都通過 mangler 運行,這是一條至少從 2002 年以來一直保持不變的代碼路徑.

                  I've checked the compiler source and all names are run through the mangler, a code path that has remained the same since 2002 at least.

                  是的,類屬性和實例屬性是正確的術語.類屬性始終是共享的,但是將實例上的屬性分配給 to 會分配給該實例.改變列表或其他可變對象與屬性賦值不同.

                  And yes, class attributes and instance attributes are the correct terms. Class attributes are always shared, but assignment to an attribute on an instance assigns to the instance. Mutating a list or other mutable objects is not the same as attribute assignment.

                  這篇關于“私人"名稱修改和實例與類屬性的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

                  相關文檔推薦

                  python: Two modules and classes with the same name under different packages(python:不同包下同名的兩個模塊和類)
                  Configuring Python to use additional locations for site-packages(配置 Python 以使用站點包的其他位置)
                  How to structure python packages without repeating top level name for import(如何在不重復導入頂級名稱的情況下構造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(分發帶有已編譯動態共享庫的 Python 包)
                • <legend id='qhdj6'><style id='qhdj6'><dir id='qhdj6'><q id='qhdj6'></q></dir></style></legend>

                    • <bdo id='qhdj6'></bdo><ul id='qhdj6'></ul>

                      <small id='qhdj6'></small><noframes id='qhdj6'>

                        <tbody id='qhdj6'></tbody>

                        <tfoot id='qhdj6'></tfoot>

                            <i id='qhdj6'><tr id='qhdj6'><dt id='qhdj6'><q id='qhdj6'><span id='qhdj6'><b id='qhdj6'><form id='qhdj6'><ins id='qhdj6'></ins><ul id='qhdj6'></ul><sub id='qhdj6'></sub></form><legend id='qhdj6'></legend><bdo id='qhdj6'><pre id='qhdj6'><center id='qhdj6'></center></pre></bdo></b><th id='qhdj6'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='qhdj6'><tfoot id='qhdj6'></tfoot><dl id='qhdj6'><fieldset id='qhdj6'></fieldset></dl></div>
                          • 主站蜘蛛池模板: 成人国产精品视频 | 99久久精品免费视频 | 日韩成人影院在线观看 | 国产精品日韩欧美一区二区三区 | www.4567| 欧美一级片在线观看 | 日本不卡一区 | 国产yw851.c免费观看网站 | 狠狠干影院| 免费一区 | 午夜成人免费视频 | 国产一区精品 | 一区福利视频 | av第一页| 国产免费一区二区 | 久久www免费视频 | 中文字幕精品一区 | 成人午夜影院 | 超碰欧美| 日韩精品亚洲专区在线观看 | 一区二区三区四区在线视频 | 青春草在线| 男女视频在线观看 | 国产三级在线观看播放 | 国产一区二区在线视频 | 国产精品久久亚洲7777 | 久久久国产一区二区三区 | 亚洲成人中文字幕 | 精品在线一区二区三区 | 国产精品久久久久久久久久三级 | 国产剧情一区 | 国产亚洲一区二区三区在线观看 | 国产一级免费视频 | 亚洲一区二区三区在线视频 | 日韩视频一区二区 | 亚洲精品短视频 | av一区二区三区四区 | 黄色大片在线 | 91五月天 | 欧美一区二区三区四区视频 | 久久久久国产 |