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 'main.childC'>, <class 'main.childA'>, <class 'main.childB'>, <class 'main.Base'>, <type 'object'>)
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 'main.childC'>, <class 'main.childA'>, <class 'main.childB'>, <class 'main.Base'>, <type 'object'>)
从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 [<class 'main.childB'>, <class 'main.childA'>, <class 'main.Base'>, <type 'object'>]
以上是关于python类中super()和__init__()的区别说明的详细内容。更多信息请关注PHP中文网其他相关文章!

每天学习Python两个小时是否足够?这取决于你的目标和学习方法。1)制定清晰的学习计划,2)选择合适的学习资源和方法,3)动手实践和复习巩固,可以在这段时间内逐步掌握Python的基本知识和高级功能。

Python在Web开发中的关键应用包括使用Django和Flask框架、API开发、数据分析与可视化、机器学习与AI、以及性能优化。1.Django和Flask框架:Django适合快速开发复杂应用,Flask适用于小型或高度自定义项目。2.API开发:使用Flask或DjangoRESTFramework构建RESTfulAPI。3.数据分析与可视化:利用Python处理数据并通过Web界面展示。4.机器学习与AI:Python用于构建智能Web应用。5.性能优化:通过异步编程、缓存和代码优

Python在开发效率上优于C ,但C 在执行性能上更高。1.Python的简洁语法和丰富库提高开发效率。2.C 的编译型特性和硬件控制提升执行性能。选择时需根据项目需求权衡开发速度与执行效率。

Python在现实世界中的应用包括数据分析、Web开发、人工智能和自动化。1)在数据分析中,Python使用Pandas和Matplotlib处理和可视化数据。2)Web开发中,Django和Flask框架简化了Web应用的创建。3)人工智能领域,TensorFlow和PyTorch用于构建和训练模型。4)自动化方面,Python脚本可用于复制文件等任务。

Python在数据科学、Web开发和自动化脚本领域广泛应用。1)在数据科学中,Python通过NumPy、Pandas等库简化数据处理和分析。2)在Web开发中,Django和Flask框架使开发者能快速构建应用。3)在自动化脚本中,Python的简洁性和标准库使其成为理想选择。

Python的灵活性体现在多范式支持和动态类型系统,易用性则源于语法简洁和丰富的标准库。1.灵活性:支持面向对象、函数式和过程式编程,动态类型系统提高开发效率。2.易用性:语法接近自然语言,标准库涵盖广泛功能,简化开发过程。

Python因其简洁与强大而备受青睐,适用于从初学者到高级开发者的各种需求。其多功能性体现在:1)易学易用,语法简单;2)丰富的库和框架,如NumPy、Pandas等;3)跨平台支持,可在多种操作系统上运行;4)适合脚本和自动化任务,提升工作效率。

可以,在每天花费两个小时的时间内学会Python。1.制定合理的学习计划,2.选择合适的学习资源,3.通过实践巩固所学知识,这些步骤能帮助你在短时间内掌握Python。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

Atom编辑器mac版下载
最流行的的开源编辑器

SecLists
SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

PhpStorm Mac 版本
最新(2018.2.1 )专业的PHP集成开发工具

适用于 Eclipse 的 SAP NetWeaver 服务器适配器
将Eclipse与SAP NetWeaver应用服务器集成。

Dreamweaver CS6
视觉化网页开发工具