python类中super()和__init__()的区别

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python类中super()和__init__()的区别相关的知识,希望对你有一定的参考价值。

本文和大家分享的主要是python开发中super()__init__()的区别,希望通过本文的分享,对初学者学习这部分内容有所帮助。

1、单继承时super()__init__()实现的功能是类似的

class Base(object):

    def __init__(self):

        print ’Base create’

 class childA(Base):

    def __init__(self):

        print ’creat A ’,

        Base.__init__(self)

 

 class childB(Base):

    def __init__(self):

        print ’creat B ’,

        super(childB, self).__init__()

 

base = Base()

 

a = childA()

b = childB()

输出结果:

Basecreate

creat A  Basecreate

creat B  Basecreate

使用super()继承时不用显式引用基类。

 

2super()只能用于新式类中

把基类改为旧式类,即不继承任何基类

class Base():

    def __init__(self):

        print ’Base create’

执行时,在初始化b时就会报错:

super(childB, self).__init__()TypeError: mustbetype, not classobj

 

3super不是父类,而是继承顺序的下一个类

在多重继承时会涉及继承顺序,super()相当于返回继承顺序的下一个类,而不是父类,类似于这样的功能:

defsuper(class_name, self):

    mro = self.__class__.mro()

    return mro[mro.index(class_name) + 1]

mro()用来获得类的继承顺序。

例如:

class Base(object):

    def__init__(self):

        print ’Base create’

 class childA(Base):

    def__init__(self):

        print ’enter A ’

        # Base.__init__(self)

        super(childA, self).__init__()

        print ’leave A’

 

 class childB(Base):

    def__init__(self):

        print ’enter B ’

        # Base.__init__(self)

        super(childB, self).__init__()

        print ’leave B’

 class childC(childA, childB):

    pass

 

c = childC()print c.__class__.__mro__

 

输入结果如下:

enter A

enter B

Basecreate

leave B

leave A

(<class ’__main__.childC’>, <class ’__main__.childA’>, <class ’__main__.childB’>, <class ’__main__.Base’>, <type ’object’>)

supder和父类没有关联,因此执行顺序是> B>>Base

执行过程相当于:初始化childC()时,先会去调用childA的构造方法中的 super(childA, self).__init__() super(childA, self)返回当前类的继承顺序中childA后的一个类childB;然后再执行childB().__init()__,这样顺序执行下去。

在多重继承里,如果把childA()中的 super(childA, self).__init__() 换成Base.__init__(self),在执行时,继承childA后就会直接跳到Base类里,而略过了childB

enter A

Basecreate

leave A

(<class ’__main__.childC’>, <class ’__main__.childA’>, <class ’__main__.childB’>, <class ’__main__.Base’>, <type ’object’>)

super()方法可以看出,super()的第一个参数可以是继承链中任意一个类的名字,

如果是本身就会依次继承下一个类;

如果是继承链里之前的类便会无限递归下去;

如果是继承链里之后的类便会忽略继承链汇总本身和传入类之间的类;

比如将childA()中的super改为:super(childC, self).__init__(),程序就会无限递归下去。

如:

  File "C:/Users/Administrator/Desktop/crawler/learn.py", line 10, in __init__

    super(childC, self).__init__()

  File "C:/Users/Administrator/Desktop/crawler/learn.py", line 10, in __init__

    super(childC, self).__init__()

  File "C:/Users/Administrator/Desktop/crawler/learn.py", line 10, in __init__

    super(childC, self).__init__()

  File "C:/Users/Administrator/Desktop/crawler/learn.py", line 10, in __init__

    super(childC, self).__init__()

  File "C:/Users/Administrator/Desktop/crawler/learn.py", line 10, in __init__

    super(childC, self).__init__()

  File "C:/Users/Administrator/Desktop/crawler/learn.py", line 10, in __init__

    super(childC, self).__init__()

  File "C:/Users/Administrator/Desktop/crawler/learn.py", line 10, in __init__

    super(childC, self).__init__()

  File "C:/Users/Administrator/Desktop/crawler/learn.py", line 10, in __init__

    super(childC, self).__init__()

  File "C:/Users/Administrator/Desktop/crawler/learn.py", line 10, in __init__

    super(childC, self).__init__()

  File "C:/Users/Administrator/Desktop/crawler/learn.py", line 10, in __init__

    super(childC, self).__init__()

  File "C:/Users/Administrator/Desktop/crawler/learn.py", line 10, in __init__

    super(childC, self).__init__()

  File "C:/Users/Administrator/Desktop/crawler/learn.py", line 10, in __init__

    super(childC, self).__init__()

  File "C:/Users/Administrator/Desktop/crawler/learn.py", line 10, in __init__

    super(childC, self).__init__()RuntimeError: maximumrecursiondepthexceededwhile calling a Pythonobject

4super()可以避免重复调用

如果childA基础Base, childB继承childABase,如果childB需要调用Base__init__()方法时,就会导致__init__()被执行两次:

class Base(object):

    def__init__(self):

        print ’Base create’

 class childA(Base):

    def__init__(self):

        print ’enter A ’

        Base.__init__(self)

        print ’leave A’

 

 class childB(childA, Base):

    def__init__(self):

        childA.__init__(self)

        Base.__init__(self)

 

b = childB()

Base__init__()方法被执行了两次

enter A

Basecreate

leave A

Basecreate

使用super()是可避免重复调用

class Base(object):

    def__init__(self):

        print ’Base create’

 class childA(Base):

    def__init__(self):

        print ’enter A ’

        super(childA, self).__init__()

        print ’leave A’

 

 class childB(childA, Base):

    def__init__(self):

        super(childB, self).__init__()

 

b = childB()

print b.__class__.mro()

enter A

Basecreate

leave A[<class ’__main__.childB’>, <class ’__main__.childA’>, <class ’__main__.Base’>, <type ’object’>]

 

来源:伯乐在线

以上是关于python类中super()和__init__()的区别的主要内容,如果未能解决你的问题,请参考以下文章

python类中super()和__init__()的区别

为啥允许在 Python 中使用 super 从 __init__ 返回一个值?

推荐使用的派生方法:super().__init__()

python中super().__init__和类名.__init__的区别

python中super()方法的解释

Python中super()和__init__()方法