Python super关键字

super常用在子类继承父类,子类调用父类方法时,例如

class A: #注意 如果使用python2,基类如果没有继承其他类,则需要继承object,否则使用super会报错;python3不需要
    def __init__(self, a):
        self.a = a
class B(A):
    def __init__(self):
        super(B,self).__init__(1)

这里不局限于初始化,还可以调用父类其他方法。

猜你喜欢

转载自blog.csdn.net/a540366413/article/details/73965399