1
我習慣在構造函數之前聲明類中的靜態字段/變量。在Python中這樣做會導致錯誤。如何在構造函數之前聲明靜態變量,以及如何在其正文中引用類?
下面是一個例子類:
class StringCompare:
methods = OrderedDict()
# ERROR!:
#methods['equals'] = equals
#methods['ends with'] = endswith
#methods['starts with'] = startswith
#methods['contains'] = contains
@staticmethod
def equals(a, b):
return a == b
@staticmethod
def contains(a, b):
return a.find(b) != -1
@staticmethod
def startswith(a, b):
return a.startswith(b)
@staticmethod
def endswith(a, b):
return a.endswith(b)
methods['equals'] = equals
methods['ends with'] = endswith
methods['starts with'] = startswith
methods['contains'] = contains
是否有更優雅的方式(除了直接把所有語句全班後前綴來StringCompare.
所訪問的每一VAR)?
這裏最好的做法是什麼?
class Type(InlineFragment):
# primitive types get None as package name
def __init__(self, packageName, name, genericType=None):
...
def ...
primitive = {
'Character': Type(None, 'char'),
'Byte' : Type(None, 'byte'),
'Short' : Type(None, 'short'),
'Integer' : Type(None, 'int'),
'Long' : Type(None, 'long'),
'Boolean' : Type(None, 'boolean'),
'Float' : Type(None, 'float'),
'Double' : Type(None, 'double'),
}
這將導致一個錯誤:試圖從同一個類中調用構造函數時
一個更復雜的情況是
\jpa_export_fragments.py", line 361, in Type
'Character' : Type(None, 'char'),
NameError: name 'Type' is not defined
這應該工作,但我只能通過將這些代碼放在課程之外來解決這個問題。
雖然我試圖提供有用的例子,但答案是「Python是不同的,學習它的獨特風格」。在Python中,並非每件事都必須在一個類中。 'primatives'應該是'Type'定義後的模塊級變量。 – agf 2012-04-14 19:57:51