這是我寫作測試的代碼片段。我注意到,如果我不定義init方法作爲類方法,代碼不運行:爲什麼我的__init__函數需要是@classmethod?
class A(object):
def __init__(self):
self.value = 0
self.add(1)
@classmethod
def add(self, arg):
self.value += arg
class B(A):
@classmethod
def add(self, arg):
self.value += arg * 2
if __name__ == '__main__':
a = A()
b = B()
print a.value
print b.value
此輸出:
Traceback (most recent call last):
File "inherit.py", line 17, in <module>
a = A()
File "inherit.py", line 4, in __init__
self.add(1)
File "inherit.py", line 8, in add
self.value += arg
AttributeError: type object 'A' has no attribute 'value'
但是,如果我改變我的初始化函數是@classmethod
,代碼按預期工作:
class A(object):
@classmethod
def __init__(self):
self.value = 0
self.add(1)
@classmethod
def add(self, arg):
self.value += arg
class B(A):
@classmethod
def add(self, arg):
self.value += arg * 2
if __name__ == '__main__':
a = A()
b = B()
print a.value
print b.value
輸出:
1
2
我在印象之下init默認是一個類方法,它的第一個參數必須是self。到底是怎麼回事?
我不認爲你明白什麼是classmethod。默認情況下'__init__'不是一個類方法,代碼中的任何方法都不應該是類方法。 – jwodder 2014-10-16 19:18:55
它可能不像預期的那樣工作。嘗試以下操作:'a1 = A(); a2 = A(); a1.add(1);斷言a1.value == a2.value'。 – chepner 2014-10-16 19:40:06