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

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

  • <small id='0Q17i'></small><noframes id='0Q17i'>

      <bdo id='0Q17i'></bdo><ul id='0Q17i'></ul>
    <legend id='0Q17i'><style id='0Q17i'><dir id='0Q17i'><q id='0Q17i'></q></dir></style></legend>
        <tfoot id='0Q17i'></tfoot>

        Python 類屬性及其初始化

        Python class attributes and their initialization(Python 類屬性及其初始化)
      1. <tfoot id='Yhv19'></tfoot>

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

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

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

                    <tbody id='Yhv19'></tbody>
                  本文介紹了Python 類屬性及其初始化的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  我是 python 的新手,這些天我正在探索課程.我有一個關于類內的屬性和變量的問題:在類的主體中僅通過 q=1 定義屬性和通過定義 self.q=1__init__ 中的代碼>?例如,以下兩種可能性有什么區別?

                  I'm quite new in python and during these days I'm exploring classes. I have a question concerning attributes and variables inside classes: What is the difference between defining an attribute via just q=1 in the body of the class and via defining self.q=1 inside the __init__? For example, what is the difference between the following two possibilities?

                  class MyClass1:
                      q=1
                      def __init__(self,p):
                          self.p=p
                      def AddSomething(self,x):
                          self.q = self.q+x
                  

                  class MyClass2:
                      def __init__(self,p):
                          self.q=1
                          self.p=p
                      def AddSomething(self,x):
                          self.q = self.q+x
                  

                  例如的輸出:

                  >>> my=MyClass1(2)
                  >>> my.p
                  2
                  >>> my.q
                  1
                  >>> my.AddSomething(7)
                  >>> my.q
                  8
                  

                  不取決于是使用 MyClass1 還是 MyClass2.MyClass1MyClass2 都不會發生錯誤.

                  does not depend on whether MyClass1 or MyClass2 is used. Neither in MyClass1 nor in MyClass2 does an error occur.

                  推薦答案

                  python中的類及其實例使用類似字典的數據結構來存儲信息.

                  Classes as well as their instances in python uses dictionary like data structure to store the information.

                  因此,對于每個類定義,將分配一個字典來存儲類級別信息(類變量).對于該特定類的每個實例,將分配一個單獨的字典(self),其中將存儲實例特定信息(實例變量).

                  So for each class definition, a dictionary will be allocated where the class level information (class variables) will be stored. And for each instance of that particular class, a separate dictionary(self) will be allocated where the instance specific information(instance variables) will be stored.

                  所以現在下一個問題是:如何查找特定名稱??

                  So now the next question is: How the lookup for a particular name will be performed ??

                  這個問題的答案是,如果您通過某個實例訪問名稱,將首先搜索特定于實例的字典,如果在那里找不到該名稱,則將在類字典中搜索該名稱.因此,如果在兩個級別都定義了相同的值,則前一個將被覆蓋.

                  And answer to this question is that if you are accessing the names through some instance, the instance specific dictionary will be searched first and if the name is not found there, then the class dictionary will be searched for that name. So if the same value is defined at both levels that former one will be overridden.

                  請注意,當您編寫 d['key'] = val 時,其中 d 是字典,如果字典中不存在 'key',則會自動添加到該字典中.否則當前值將被覆蓋.在閱讀進一步說明之前,請記住這一點.

                  現在讓我們看看你用來描述問題的代碼:

                  Now lets go through the code you have used to describe your problem:

                  MyClass1

                  class MyClass1:
                      q=1
                      def __init__(self,p):
                          self.p=p
                      def AddSomething(self,x):
                          self.q = self.q+x
                  

                  <小時>

                  1. my = Myclass1(2) #create new instance and add variables to it.
                  
                      MyClass = {"q" : 1}
                      my = {"p" : 2}
                  

                  <小時>

                  2. my.p    # =2, p will be taken from Dictionary of my-instance.
                  

                  <小時>

                  3. my.q    # =1, q will be takn from MyClass dict. (Not present in dictionary of my-instance).
                  

                  <小時>

                  4. my.AddSomething(7) # This method access the value of q (using self.q) first 
                                        # which is not defined in my dict and hence will be taken
                                        # from dictionary of MyClass. After the addition operation,
                                        # the sum is being stored in self.q. Note that now we are
                                        # adding the name q to Dictionary of my-instance and hence                   
                                        # a new memory space will be created in Dictionary of my-instance
                                        # and the future references to self.q will fetch the value
                                        # of self.q from dictionary of my-instance.
                  
                      MyClass = {"q" : 1}
                      my = {"p" : 2, "q" : 8}
                  

                  <小時>

                  5. my.q   # =8, q now is available in dictionary of my-instance.
                  

                  這篇關于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 包)
                    <tbody id='BPAtg'></tbody>
                  1. <tfoot id='BPAtg'></tfoot>

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

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

                            主站蜘蛛池模板: 色综合久久久久 | 成人精品视频在线观看 | 亚洲国产成人精品久久久国产成人一区 | 欧洲国产精品视频 | 亚洲高清网 | 久久免费观看视频 | 久久日韩精品 | 91免费版在线观看 | 免费视频一区二区 | av一级久久 | 91精品国产美女在线观看 | 一区二区三区四区国产 | 国产欧美精品一区二区三区 | 夜夜骑首页 | 久久精品99国产精品 | 成人在线视频免费播放 | 91精品国产91久久久久久吃药 | 国产精品日韩欧美 | 亚洲欧美精品 | www.久久精品 | 欧美激情 一区 | 国产精品一区二区在线免费观看 | 欧美aaa| yiren22综合网成人 | av男人天堂影院 | 国产91久久久久蜜臀青青天草二 | 成人免费在线观看视频 | 亚洲精品一区二区网址 | 色www精品视频在线观看 | 亚洲一区二区在线播放 | 日韩精品一区二区三区中文在线 | a免费视频| 91视频在线看 | 精品国产99| 亚洲高清电影 | 成人 在线 | 欧美成人精品一区二区男人看 | 成人欧美一区二区三区在线播放 | 亚洲精品一区二三区不卡 | 成人欧美一区二区 | 夜夜爽99久久国产综合精品女不卡 |