访问已被覆盖的继承方法

时间:2011-11-24 16:07:14

标签: python

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'

为什么?

1 个答案:

答案 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()