您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

python类super()和__init__()区别示例

5b51 2022/1/14 8:14:56 python 字数 10299 阅读 302 来源 www.jb51.cc/python

python类super()和__init__()区别示例

概述

最近有同学问我关于Python类中的super()__init__()共同点和不同点的问题, 我今天把它们两个的异同点总结了一下,希望可以帮助遇到同样困惑的同学。


# @param python类中super()__init__()的区别
# @author 编程之家 jb51.cc|www.www.jb51.cc 

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

# End www.jb51.cc

输出结果:


Base create
creat A  Base create
creat B  Base create

# End www.jb51.cc

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

 

 

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


# @param python类中super()__init__()的区别
# @author 编程之家 jb51.cc|www.www.jb51.cc 

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

# End www.jb51.cc

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


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

# End www.jb51.cc

 

 

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


# @param python类中super()__init__()的区别
# @author 编程之家 jb51.cc|www.www.jb51.cc 

def super(class_name,self):
    mro = self.__class__.mro()
    return mro[mro.index(class_name) + 1]

# End www.jb51.cc

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


# @param python类中super()__init__()的区别
# @author 编程之家 jb51.cc|www.www.jb51.cc 

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__

# End www.jb51.cc

输出结果如下:


enter A 
enter B 
Base create
leave B
leave A
(,)

# End www.jb51.cc

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
(,)

# End www.jb51.cc

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

 

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

 

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

 

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

 

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


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

# End www.jb51.cc

 

 

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


# @param python类中super()__init__()的区别
# @author 编程之家 jb51.cc|www.www.jb51.cc 

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

# End www.jb51.cc

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


enter A 
Base create
leave A
Base create

# End www.jb51.cc

 

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


# @param python类中super()__init__()的区别
# @author 编程之家 jb51.cc|www.www.jb51.cc 

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 
Base create
leave A
[,]

# End www.jb51.cc

总结

以上是编程之家为你收集整理的python类super()和__init__()区别示例全部内容,希望文章能够帮你解决python类super()和__init__()区别示例所遇到的程序开发问题。


如果您也喜欢它,动动您的小指点个赞吧

除非注明,文章均由 laddyq.com 整理发布,欢迎转载。

转载请注明:
链接:http://laddyq.com
来源:laddyq.com
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


联系我
置顶