前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python类中super()和__init__()的关系

Python类中super()和__init__()的关系

作者头像
狼啸风云
修改2022-09-02 21:51:32
7210
修改2022-09-02 21:51:32
举报
文章被收录于专栏:计算机视觉理论及其实现

Python类中super()和__init__()的关系

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

代码语言:javascript
复制
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()


Output:
--------------------------
Base create
creat A  Base create
creat B  Base create
--------------------------
代码语言:javascript
复制

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

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

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

代码语言:javascript
复制
class Base():
    def __init__(self):
        print 'Base create'

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

  super(childB, self).__init__()


Output:
---------------------------------------
TypeError: must be type, not classobj
---------------------------------------

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

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

代码语言:javascript
复制
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__

Output:
-------------------------------------------------------------------------------------------
enter A 
enter B 
Base create
leave B
leave A
(<class '__main__.childC'>, <class '__main__.childA'>, <class '__main__.childB'>, <class '__main__.Base'>, <type 'object'>)
-------------------------------------------------------------------------------------------

super和父类没有关联,因此执行顺序是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:

代码语言:javascript
复制
enter A 
Base create
leave A
(<class '__main__.childC'>, <class '__main__.childA'>, <class '__main__.childB'>, <class '__main__.Base'>, <type 'object'>)

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

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

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

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

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

如:

代码语言:javascript
复制
 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: maximum recursion depth exceeded while calling a Python object

4. super()避免重复调用

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

代码语言:javascript
复制
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__()方法被执行了两次



Output:
--------------
enter A 
Base create
leave A
Base create
--------------

 使用super()避免重复调用,如下:

代码语言:javascript
复制
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()


Output:


#输出:
-----------------------------------------------------------------------------------------
enter A 
Base create
leave A
[<class '__main__.childB'>, <class '__main__.childA'>, <class '__main__.Base'>, <type 'object'>]
-----------------------------------------------------------------------------------------
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020/03/27 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1.单继承时super()和__init__()实现的功能是类似的
  • 2. super()只能用于新式类中。
  • 3. super不是父类,而是继承顺序的下一个类
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档