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

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

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

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

      <tfoot id='FkbJ7'></tfoot>
        <bdo id='FkbJ7'></bdo><ul id='FkbJ7'></ul>

        Python 類與模塊屬性

        Python Class vs. Module Attributes(Python 類與模塊屬性)

          <legend id='0OfXT'><style id='0OfXT'><dir id='0OfXT'><q id='0OfXT'></q></dir></style></legend>

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

        • <tfoot id='0OfXT'></tfoot>
            <tbody id='0OfXT'></tbody>
                <bdo id='0OfXT'></bdo><ul id='0OfXT'></ul>

                  <small id='0OfXT'></small><noframes id='0OfXT'>

                  本文介紹了Python 類與模塊屬性的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

                  問題描述

                  我有興趣聽到一些關(guān)于 Python 中的類屬性的討論.例如,什么是類屬性的好用例?在大多數(shù)情況下,我想不出一個(gè)類屬性比使用模塊級(jí)屬性更可取的情況.如果這是真的,那為什么還有他們呢?

                  I'm interested in hearing some discussion about class attributes in Python. For example, what is a good use case for class attributes? For the most part, I can not come up with a case where a class attribute is preferable to using a module level attribute. If this is true, then why have them around?

                  我遇到的問題是,錯(cuò)誤地破壞類屬性值幾乎太容易了,然后你的全局"值變成了本地實(shí)例屬性.

                  The problem I have with them, is that it is almost too easy to clobber a class attribute value by mistake, and then your "global" value has turned into a local instance attribute.

                  請(qǐng)隨意評(píng)論您將如何處理以下情況:

                  Feel free to comment on how you would handle the following situations:

                  1. 類和/或子類使用的常量值.這可能包括永遠(yuǎn)不會(huì)改變但可能需要一次性初始化的幻數(shù)"字典鍵或列表索引.
                  2. 默認(rèn)類屬性,在極少數(shù)情況下為類的特殊實(shí)例更新.
                  3. 用于表示在所有實(shí)例之間共享的類的內(nèi)部狀態(tài)的全局?jǐn)?shù)據(jù)結(jié)構(gòu).
                  4. 初始化許多默認(rèn)屬性的類,不受構(gòu)造函數(shù)參數(shù)的影響.

                  一些相關(guān)帖子:
                  類和實(shí)例屬性的區(qū)別

                  推薦答案

                  #4:我從不使用類屬性來初始化默認(rèn)實(shí)例屬性(通常放在 __init__ 中的那些).例如:

                  #4: I never use class attributes to initialize default instance attributes (the ones you normally put in __init__). For example:

                  class Obj(object):
                      def __init__(self):
                          self.users = 0
                  

                  永遠(yuǎn)不會(huì):

                  class Obj(object):
                      users = 0
                  

                  為什么?因?yàn)樗遣灰恢碌?當(dāng)您分配除不變對(duì)象之外的任何東西時(shí),它不會(huì)做您想做的事情:

                  Why? Because it's inconsistent: it doesn't do what you want when you assign anything but an invariant object:

                  class Obj(object):
                      users = []
                  

                  使用戶列表在所有對(duì)象之間共享,在這種情況下,這是不希望的.根據(jù)它們的類型將它們拆分為 __init__ 中的類屬性和賦值會(huì)令人困惑,因此我總是將它們?nèi)糠旁?__init__ 中,反正我覺得這樣更清楚.

                  causes the users list to be shared across all objects, which in this case isn't wanted. It's confusing to split these into class attributes and assignments in __init__ depending on their type, so I always put them all in __init__, which I find clearer anyway.

                  至于其余部分,我通常將特定于類的值放在類中.這并不是因?yàn)槿肿兞渴切皭旱?——它們不像在某些語言中那么重要,因?yàn)樗鼈內(nèi)匀痪窒抻谀K,除非模塊本身太大——但是如果外部代碼想要訪問它們,將所有相關(guān)值放在一個(gè)地方很方便.比如在module.py中:

                  As for the rest, I generally put class-specific values inside the class. This isn't so much because globals are "evil"--they're not so big a deal as in some languages, because they're still scoped to the module, unless the module itself is too big--but if external code wants to access them, it's handy to have all of the relevant values in one place. For example, in module.py:

                  class Obj(object):
                      class Exception(Exception): pass
                      ...
                  

                  然后:

                  from module import Obj
                  
                  try:
                      o = Obj()
                      o.go()
                  except o.Exception:
                      print "error"
                  

                  除了允許子類更改值(這并不總是需要)之外,這意味著我不必費(fèi)力地導(dǎo)入異常名稱和使用 Obj 所需的一堆其他東西."from module import Obj, ObjException, ..." 很快就會(huì)讓人厭煩.

                  Aside from allowing subclasses to change the value (which isn't always wanted anyway), it means I don't have to laboriously import exception names and a bunch of other stuff needed to use Obj. "from module import Obj, ObjException, ..." gets tiresome quickly.

                  這篇關(guān)于Python 類與模塊屬性的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

                  【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(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 包)

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

                            主站蜘蛛池模板: 少妇特黄a一区二区三区88av | 综合婷婷| 黄色一级大片在线免费看产 | av av在线 | 日韩视频免费看 | 天堂一区二区三区 | 黄色在线免费网站 | 9191在线播放 | 一区二区三区免费在线观看 | 四虎永久免费在线 | 天天综合操| 国产99久久精品 | 免费视频一区二区 | 欧美婷婷| 国产欧美日韩在线播放 | 久久aⅴ乱码一区二区三区 亚洲欧美综合精品另类天天更新 | 在线观看成人 | 午夜丰满少妇一级毛片 | 天堂色综合 | 精品久久久久久久久久久久久 | 国产精品视频在线免费观看 | 中文字幕精品视频 | 免费亚洲一区二区 | 亚洲国产精品一区在线观看 | 亚洲精品一区二区三区中文字幕 | xx性欧美肥妇精品久久久久久 | 干干干操操操 | 免费一级黄色录像 | 黄色香蕉视频在线观看 | 成人免费观看视频 | 日韩中文字幕高清 | a在线观看 | 午夜寂寞福利视频 | 男女污污动态图 | 久久久久久成人网 | 国产欧美一区二区三区免费 | www.亚洲精品 | 亚洲一区二区三区免费在线观看 | 亚洲精品三级 | 日本黄视频在线观看 | 日本国产高清 |