首页 >后端开发 >Python教程 >关于python类中super()和__init__()的区别说明

关于python类中super()和__init__()的区别说明

高洛峰
高洛峰原创
2017-03-16 16:08:291408浏览

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

输出结果:

Base create
creat A  Base create
creat B  Base create

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


super()只能用于新式类中


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

class Base():
    def init(self):
        print 'Base create'

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

super(childB, self).init()
TypeError: must be type, not classobj


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


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

def super(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 
Base create
leave B
leave A
(<class &#39;main.childC&#39;>, <class &#39;main.childA&#39;>, <class &#39;main.childB&#39;>, <class &#39;main.Base&#39;>, <type &#39;object&#39;>)

supder和父类没有关联,因此执行顺序是A —> 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 
Base create
leave A
(<class &#39;main.childC&#39;>, <class &#39;main.childA&#39;>, <class &#39;main.childB&#39;>, <class &#39;main.Base&#39;>, <type &#39;object&#39;>)

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


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


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


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


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

  File "test.py", line 12, in init
    super(childC, self).init()
  File "test.py", line 12, in init
    super(childC, self).init()
  File "test.py", line 12, in init
    super(childC, self).init()
  File "test.py", line 12, in init
    super(childC, self).init()
  File "test.py", line 12, in init
    super(childC, self).init()
  File "test.py", line 12, in init
    super(childC, self).init()
  File "test.py", line 12, in init
    super(childC, self).init()
  File "test.py", line 12, in init
    super(childC, self).init()
  File "test.py", line 12, in init
    super(childC, self).init()
RuntimeError: maximum recursion depth exceeded while calling a Python object


super()可以避免重复调用


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

class Base(object):
    def init(self):
        print &#39;Base create&#39;
class childA(Base):
    def init(self):
        print &#39;enter A &#39;
        Base.init(self)
        print &#39;leave A&#39;
class childB(childA, Base):
    def init(self):
        childA.init(self)
        Base.init(self)
b = childB()

Base的init()方法被执行了两次

enter A 
Base create
leave A
Base create


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

class Base(object):
    def init(self):
        print &#39;Base create&#39;
class childA(Base):
    def init(self):
        print &#39;enter A &#39;
        super(childA, self).init()
        print &#39;leave A&#39;
class childB(childA, Base):
    def init(self):
        super(childB, self).init()
b = childB()
print b.class.mro()
enter A 
Base create
leave A
[<class &#39;main.childB&#39;>, <class &#39;main.childA&#39;>, <class &#39;main.Base&#39;>, <type &#39;object&#39;>]


以上是关于python类中super()和__init__()的区别说明的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn