栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

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

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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


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

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

从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 '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 
base create
leave A
base create


使用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 
base create
leave A
[, , , ]

原文来源:https://m.pythontab.com/article/1108


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/220756.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号