class A:
def start(self):
pass
class B(A):
def start(self):
super(A,self).start()
b = B()
b.start()
给出了这个错误:
Traceback (most recent call last):
File "C:/Python32/a.py", line 10, in <module>
b.start()
File "C:/Python32/a.py", line 7, in start
super(A,self).start()
AttributeError: 'super' object has no attribute 'start'
为什么?
答案 0 :(得分:7)
如python doc中所述,super仅适用于新样式类,即:
注意 super()仅适用于新式类。
所以你应该这样做:
class A(object): # inherit from object to be a new style class
def start(self):
pass
class B(A):
def start(self):
super(B,self).start() # the first arg of super is the current class
b = B()
b.start()