I see everywhere examples that super-class methods should be called by:

super(SuperClass, instance).method(args)

Is there any disadvantage to doing:

SuperClass.method(instance, args)

解决方案

Consider the following situation:

class A(object):

def __init__(self):

print('Running A.__init__')

super(A,self).__init__()

class B(A):

def __init__(self):

print('Running B.__init__')

# super(B,self).__init__()

A.__init__(self)

class C(A):

def __init__(self):

print('Running C.__init__')

super(C,self).__init__()

class D(B,C):

def __init__(self):

print('Running D.__init__')

super(D,self).__init__()

foo=D()

So the classes form a so-called inheritance diamond:

A

/ \

B C

\ /

D

Running the code yields

Running D.__init__

Running B.__init__

Running A.__init__

That's bad because C's __init__ is skipped. The reason for that is because B's __init__ calls A's __init__ directly.

The purpose of super is to resolve inheritance diamonds. If you un-comment

# super(B,self).__init__()

and comment-out

A.__init__(self)

the code yields the more desireable result:

Running D.__init__

Running B.__init__

Running C.__init__

Running A.__init__

Now all the __init__ methods get called. Notice that at the time you define B.__init__ you might think that super(B,self).__init__() is the same as calling A.__init__(self), but you'd be wrong. In the above situation, super(B,self).__init__() actually calls C.__init__(self).

Holy smokes, B knows nothing about C, and yet super(B,self) knows to call C's __init__? The reason is because self.__class__.mro() contains C. In other words, self (or in the above, foo) knows about C.

So be careful -- the two are not fungible. They can yield vastly different results.

Using super has pitfalls. It takes a considerable level of coordination between all the classes in the inheritance diagram. (They must, for example, either have the same call signature for __init__, since any particular __init__ would not know which other __init__ super might call next, or

else use **kwargs.) Furthermore, you must be consistent about using super everywhere. Skip it once (as in the above example) and you defeat the entire purpose of super.

See the link for more pitfalls.

If you have full control over your class hierarchy, or you avoid inheritance diamonds, then there is no need for super.

python替代技术,Python超级方法和调用替代品相关推荐

  1. Python实战技术 - Python虚拟隔离环境 和 Docker技术

    Python实战技术 - Python虚拟隔离环境 和 Docker技术 已经学过,只想查查手册?--试试直接跳转到命令: (1)使用venv创建相关命令 (2)使用virtualenv创建相关命令 ...

  2. Python基础编程——多重继承下方法的调用

    我们介绍了在继承下面调用父类的构造方法,那么在多重继承下如何调用父类的方法呢?虽然在使用多重继承时需要慎重,但是在实际的项目开发中多重继承却随处可见,因此了解多重继承下调用父类的方法也是很有必要的. ...

  3. python hook技术,python hook监听事件详解

    本文实例为大家分享了python hook监听事件的具体代码,供大家参考,具体内容如下 # -*- coding: utf-8 -*- # # by oldj http://oldj.net/ # i ...

  4. python替代php,Python架构的PHP替代方案

    I am happily using fabric for my Python projects for deployment. Now I am engaged in a larger PHP pr ...

  5. python画管柱图_(伍晓平)Python编程技术[Python数据分析与展示

    更多相关问题 C++中,每个对象都是由 和 这两部分组成的. 关于小儿维生素D缺乏性佝偻病的概念,以下叙述不正确的是( ) 细胞学说的内容包括? 双打比赛中,接球员甲准备接球时,其同伴丙可以任意站位, ...

  6. matlab下调用python,numpy库函数的方法

    matlab下调用python,numpy库函数的方法 Matlab 调用 Python 下的脚本 这篇博文的目的是为了解决博主在用matlab的eig函数时遇到的一个问题: 博主在用matlab下的 ...

  7. Python 同一个类中不同函数相互调用

    本文采取:Python的类中函数方法相互调用的两种方式_狗哥的博客-CSDN博客 __metaclass__=type class Stu:name=Noneage=Noneschool=" ...

  8. python服务器稳定性,一种基于Python服务器稳定性测试的方法技术

    [技术实现步骤摘要] 本专利技术涉及一种服务器稳定性测试的方法,具体地说是. 技术介绍 随着服务器行业的快速发展,越来越多的客户开始偏向于购买大批量的服务器作为自己的核心应用.所以对服务器的稳定性也提 ...

  9. Python面向对象程序设计中对象析构方法的调用时机

    开学第一课:一定不要这样问老师Python问题 中国大学MOOC"Python程序设计基础"第6次开课时间 董付国老师Python系列教材推荐与选用参考 ============= ...

最新文章

  1. 三星电池正在获取使用模式_三星Galaxy S10系列超大电池持久动力,解决电量烦恼...
  2. php 生成非对称密钥,php实现非对称加密
  3. Java 里的 for (;;) 与 while (true),哪个更快?
  4. win10使用python的strftime有错误_17个常见的Python运行时错误
  5. sleep头文件linux,Linux下的sleep函数 要用的话得包涵什么头文件啊?
  6. dataTables基础函数变量
  7. 使用SQL脚本创建数据库,操作主键、外键与各种约束(MS SQL Server)
  8. Alwayson--问题总结二
  9. 《Java EE 7精粹》—— 第3章 JSF 3.1 Facelets
  10. jQuery的Deferred
  11. 关于微信服务商统一下单需要注意的几个问题
  12. C++关键字 explicit
  13. Redis源码分析系列十一:createClient后面内容
  14. 2016年APP推广应该怎么做?
  15. 远程计算机关机了怎么办,远程关机的详细步骤有哪些?向日葵怎么远程关机?...
  16. excel保存csv文件数字失真解决办法
  17. 编辑网页document.body.contentEditable=‘true‘;
  18. getcwd()函数的用法
  19. xp oracle10g安装图解,虚拟机xp系统中Oracle 10g的安装
  20. 同城聚合平台v59.4.0 本地同城 同城信息 同城商家

热门文章

  1. 出现Too many connections错误,怎样解决?
  2. 让文本输入框只能输入数字
  3. 学习3dmax的第二天
  4. ubuntu 启动时 设置多个ip 修改mac 网关 。。。
  5. gj7 对象引用、可变性和垃圾回收
  6. 基于java人体检测技术_几篇深度图人体检测论文的实现
  7. lambdaquerywrapper查询指定字段_MongoDB系列 | 高级查询与索引(四)
  8. 用户登陆_「python学习笔记」用户登陆需求实现(for/if/str知识点)
  9. 手把手教你将pyqt程序打包成exe(1)
  10. 1276: 求和游戏