Skip to main content
 首页 » 编程设计

python之访问已被覆盖的继承方法

2025年05月04日17lyj

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' 

为什么?

请您参考如下方法:

如 python doc 中所述, super 仅适用于新样式类,即:

Note super() only works for new-style classes.

所以你应该这样做:

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