≡
  • 网络编程
  • 数据库
  • CMS技巧
  • 软件编程
  • PHP笔记
  • JavaScript
  • MySQL
位置:首页 > 网络编程 > Python

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

人气:528 时间:2018-09-29

这篇文章主要为大家详细介绍了python类super()和__init__()区别示例,具有一定的参考价值,可以用来参考一下。

对python这个高级语言感兴趣的小伙伴,下面一起跟随四海网的小编两巴掌来看看吧!

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

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


# @param python类中super()和__init__()的区别
# @author 四海网|www.q1010.com 

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_512pic_com

输出结果:


Base create
creat A  Base create
creat B  Base create

# End www_512pic_com

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

 

super()只能用于新式类中

 

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


# @param python类中super()和__init__()的区别
# @author 四海网|www.q1010.com 

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

# End www_512pic_com

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


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

# End www_512pic_com

 

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

 

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


# @param python类中super()和__init__()的区别
# @author 四海网|www.q1010.com 

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

# End www_512pic_com

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


# @param python类中super()和__init__()的区别
# @author 四海网|www.q1010.com 

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_512pic_com

输出结果如下:


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

# End www_512pic_com

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_512pic_com

从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

# End www_512pic_com

 

super()可以避免重复调用

 

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


# @param python类中super()和__init__()的区别
# @author 四海网|www.q1010.com 

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_512pic_com

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


enter A 
Base create
leave A
Base create

# End www_512pic_com

 

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


# @param python类中super()和__init__()的区别
# @author 四海网|www.q1010.com 

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_512pic_com

本文来自:http://www.q1010.com/181/1892-0.html

注:关于python类super()和__init__()区别示例的内容就先介绍到这里,更多相关文章的可以留意四海网的其他信息。

关键词:super(),__init__()

您可能感兴趣的文章

上一篇:Python局部变量和全局变量用法示例
下一篇:Python-nmap网络扫描和嗅探工具包使用示例
热门文章
  • Python 处理Cookie的菜鸟教程(一)Cookie库
  • python之pandas取dataframe特定行列的简单示例
  • Python解决json.dumps错误::‘utf8’ codec can‘t decode byte
  • Python通过pythony连接Hive执行Hql的脚本
  • Python 三种方法删除列表中重复元素的简单示例
  • python爬虫代码示例
  • Python 中英文标点转换示例
  • Python 不得不知的开源项目解析
  • Python urlencode编码和url拼接实现方法
  • python按中文拆分中英文混合字符串的简单示例
  • 最新文章
    • Python利用numpy三层神经网络的简单示例
    • pygame可视化幸运大转盘的简单示例
    • Python爬虫之爬取二手房信息的简单示例
    • Python之time库的简单示例
    • OpenCV灰度、高斯模糊、边缘检测的简单示例
    • Python安装Bs4及使用的简单示例
    • django自定义manage.py管理命令的简单示例
    • Python之matplotlib 向任意位置添加一个子图(axes)的简单示例
    • Python图像标签标注软件labelme分析的简单示例
    • python调用摄像头并拍照发邮箱的简单示例

四海网收集整理一些常用的php代码,JS代码,数据库mysql等技术文章。