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

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

<tfoot id='A9SSK'></tfoot>

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

        <bdo id='A9SSK'></bdo><ul id='A9SSK'></ul>
        <legend id='A9SSK'><style id='A9SSK'><dir id='A9SSK'><q id='A9SSK'></q></dir></style></legend>

      1. 為什么從類中訪問類變量需要“self".在 Pyth

        Why accessing to class variable from within the class needs quot;self.quot; in Python?(為什么從類中訪問類變量需要“self.在 Python 中?)

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

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

                • 本文介紹了為什么從類中訪問類變量需要“self".在 Python 中?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  可能重復:
                  Python自我"解釋

                  我正在學習 Python,我有一個關于從此類的方法訪問類變量的問題,理論多于實踐.

                  I'm learning Python and I have a question, more theoretical than practical, regarding access class variables from method of this class.

                  例如我們有:

                  class ExampleClass:
                      x = 123
                      def example_method(self):
                          print(self.x)
                  

                  為什么一定要準確地寫出self.x,而不僅僅是x?x 屬于類的命名空間,使用它的方法也屬于它.我錯過了什么?這種風格背后的理由是什么?

                  Why is necessarily to write exactly self.x, not just x? x belongs to namespace of the class, and method using it belongs to it too. What am I missing? What a rationale stands behind such style?

                  在 C++ 中你可以這樣寫:

                  In C++ you can write:

                  class ExampleClass {
                  public:
                      int x;
                      void example_method()
                      {
                          x = 123;
                          cout << x;
                      };
                  };
                  

                  它會起作用的!

                  推薦答案

                  來自 Python 的歷史:添加對用戶定義類的支持:

                  相反,我決定放棄隱式引用實例變量.像 C++ 這樣的語言可以讓你把 this->foo 寫成顯式引用實例變量 foo(如果有單獨的局部變量 foo).因此,我決定做出這樣的明確引用引用實例變量的唯一方法.此外,我決定不要讓當前對象(t??his")成為特殊關鍵字,我會簡單地使this"(或其等價物)方法的第一個命名參數.實例變量總是被引用為該參數的屬性.

                  Instead, I decided to give up on the idea of implicit references to instance variables. Languages like C++ let you write this->foo to explicitly reference the instance variable foo (in case there’s a separate local variable foo). Thus, I decided to make such explicit references the only way to reference instance variables. In addition, I decided that rather than making the current object ("this") a special keyword, I would simply make "this" (or its equivalent) the first named argument to a method. Instance variables would just always be referenced as attributes of that argument.

                  使用顯式引用,不需要特殊的語法對于方法定義也不必擔心復雜關于變量查找的語義.相反,一個簡單的定義第一個參數對應于實例的函數,通過約定被命名為自我".例如:

                  With explicit references, there is no need to have a special syntax for method definitions nor do you have to worry about complicated semantics concerning variable lookup. Instead, one simply defines a function whose first argument corresponds to the instance, which by convention is named "self." For example:

                  def spam(self,y):
                      print self.x, y
                  

                  這種方法類似于我在 Modula-3 中看到的方法,它具有已經為我提供了導入和異常處理的語法.Modula-3 沒有類,但它允許您創建記錄類型包含已初始化的全類型函數指針成員默認為附近定義的函數,并添加語法糖,所以如果 x 是這樣一個記錄變量,而 m 是一個函數指針該記錄的成員,初始化為函數 f,然后調用x.m(args) 等價于調用 f(x, args).這符合對象和方法的典型實現,并使其成為可能將實例變量等同于第一個參數的屬性.

                  This approach resembles something I had seen in Modula-3, which had already provided me with the syntax for import and exception handling. Modula-3 doesn’t have classes, but it lets you create record types containing fully typed function pointer members that are initialized by default to functions defined nearby, and adds syntactic sugar so that if x is such a record variable, and m is a function pointer member of that record, initialized to function f, then calling x.m(args) is equivalent to calling f(x, args). This matches the typical implementation of objects and methods, and makes it possible to equate instance variables with attributes of the first argument.

                  因此,BDFL 本人表示,他決定使用顯式自我而不是隱式自我的唯一真正原因是:

                  So, stated by the BDFL himself, the only real reason he decided to use explicit self over implicit self is that:

                  • 這是明確的
                  • 它更容易實現,因為查找必須在運行時完成(而不是像其他語言那樣在編譯時完成),并且使用隱式 self 可能會增加查找的復雜性(從而增加成本).

                  Python FAQ中也有答案.

                  There is also an answer in the Python FAQ.

                  這篇關于為什么從類中訪問類變量需要“self".在 Python 中?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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 包)
                    1. <small id='ZWJtD'></small><noframes id='ZWJtD'>

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

                            主站蜘蛛池模板: 国产精品成人av | 国产精品精品久久久 | 久久久久久女 | 国产在线一| 国产精品久久久久久久久久久久 | 亚洲日韩中文字幕一区 | 国产一区二区三区高清 | 欧美亚洲国产日韩 | 国产精品视频网站 | 久久99精品久久久久久国产越南 | 三级av网址 | 日本a∨精品中文字幕在线 亚洲91视频 | 97精品国产手机 | 九九热九九 | 国产精品久久久久久久久 | 亚洲一区中文字幕 | 人人做人人澡人人爽欧美 | 久久久青草婷婷精品综合日韩 | 伊人精品在线 | 婷婷久久五月 | 黄色日批视频 | 久久综合九九 | 国产精品久久久久久久 | 国产精品久久久亚洲 | 欧美激情va永久在线播放 | 精品av天堂毛片久久久借种 | 国产探花在线观看视频 | 不卡一区| 亚洲精品白浆高清久久久久久 | 91国产精品 | 国产精品久久久久久久久久久久久 | 成人国产精品久久 | h在线 | 99re在线视频免费观看 | 亚洲精品第一国产综合野 | 成人中文字幕av | 日韩av成人 | 欧洲性生活视频 | 日韩精品极品视频在线观看免费 | 欧美一卡二卡在线观看 | 精品国产乱码久久久久久影片 |