問題描述
我有興趣聽到一些關(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:
- 類和/或子類使用的常量值.這可能包括永遠(yuǎn)不會(huì)改變但可能需要一次性初始化的幻數(shù)"字典鍵或列表索引.
- 默認(rèn)類屬性,在極少數(shù)情況下為類的特殊實(shí)例更新.
- 用于表示在所有實(shí)例之間共享的類的內(nèi)部狀態(tài)的全局?jǐn)?shù)據(jù)結(jié)構(gòu).
- 初始化許多默認(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)!