Python程序员

python使用super()时遇到的问题

2016-06-05  本文已影响112人  大阿拉伯人

当我们在python的子类中调用父类的方法时,会用到super(),不过我遇到了一个问题,顺便记录一下。

比如,我写了如下错误代码:

class A():
    def dosomething(self):
        print "It's A"

class B(A):    
    def dosomething(self):        
        super(B, self).dosomething()

if __name__ == '__main__':    
    b = B()    
    b.dosomething()

输出报错:

TypeError: must be type, not classobj

google之后发现,python在2.2前后有新式类(new-style class)与旧式类(old-style class)的区别,我上面的例子为旧式类,而super关键字必须作用于新式类。

新式类:所有类都必须要有继承的类,如果什么都不想继承,就继承到object类。

所以,解决此错误必须将A类改为class A(object):

上一篇 下一篇

猜你喜欢

热点阅读