super().__init__()

super().init()就是继承父类的init方法,同样也可以使用super()去继承其他方法

举个例子

class Father():
    def __init__(self,name='father'):
        self.name=name

class Son(Father):
    pass

class Son_init(Father):
    def __init__(self,age):
        self.age=age

class Son_super(Father):
    def __init__(self,name,age):
        super().__init__(name)
        self.age=age

son=Son()
son_i=Son_init(10)
son_s=Son_super('son_s',12)


print(son.name) # ouput: father
print(son_i.name) # error no attribute 'name'
print(son_s.name) # output: son_s

Python3.x 和 Python2.x 的一个区别: Python 3 可以使用直接使用 super().xxx 代替 super(Class, self).xxx

posted @ 2021-11-18 12:32  梦想家肾小球  阅读(168)  评论(0编辑  收藏  举报