这是我写作测试的代码片段。我注意到,如果我不定义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