python之super的使用小结


Posted in Python onAugust 13, 2018

为什么需要super

在python没有引入super之前, 如果需要在子类中引用父类的方法, 一般写法如下:

class Father:
 def whoami(self):
  print("I am father")


class Child:
 def whoami(self):
  print("I am child")
  Father.whoami(self)

这样看好像没什么问题, 就算没有super也能正常调用父类的方法, 但是如果有一天Father类需要修改类名为Father1, 那么子类Child中也必须跟着修改.

想象下如果一个类有很多个子类, 这样一来我们就需要修改每个子类中引用父类的语句

怎么使用super

Help on class super in module builtins:

class super(object)
 | super() -> same as super(__class__, <first argument>)
 | super(type) -> unbound super object
 | super(type, obj) -> bound super object; requires isinstance(obj, type)
 | super(type, type2) -> bound super object; requires issubclass(type2, type)
 | Typical use to call a cooperative superclass method:
 | class C(B):
 |   def meth(self, arg):
 |     super().meth(arg)
 | This works for class methods too:
 | class C(B):
 |   @classmethod
 |   def cmeth(cls, arg):
 |     super().cmeth(arg)

我们来看看super的帮助文档, 首先super是一个类, 它的调用方法有如下几种:

1.super()
2.super(type)
3.super(type, obj)
4.super(type, type2)

我们推荐用第一种方法来使用super, 因为它并不需要显式传递任何参数.但是要注意一点, super只能在新式类中使用.

class A:
 def __init__(self):
  print("this is A")

class B(A):
 def __init__(self):
  super().__init__()
  print("this is B")

b = B()

"""
this is A
this is B
"""

看起来super就像直接调用了B的父类A的__init__, 其实并不是这样的, 我们看看super在多继承下的使用

class A:
 def __init__(self):
  print("this is A")
  print("leave A")

class B(A):
 def __init__(self):
  print("this is B")
  super().__init__()
  print("leave B")

class C(A):
 def __init__(self):
  print("this is C")
  super().__init__()
  print("leave C")
 

class D(B, C):
 def __init__(self):
  print("this is D")
  super().__init__()
  print("leave D")  
  
d = D()

"""
this is D
this is B
this is C
this is A
leave A
leave C
leave B
leave D
"""

print(D.__mro__)
"""
(<class '__main__.D'>, 
<class '__main__.B'>, 
<class '__main__.C'>, 
<class '__main__.A'>, 
<class 'object'>)
"""

这里可以看到, 如果super只是简单调用父类的方法的话, 那么调用了B的__init__ 方法之后, 应该调用A的__init__ 才对, 但是调用的却是C的__init__ 方法

这是因为super调用的是当前类在MRO列表中的后一个类, 而并不是简单地调用当前类的父类

python并没有强制性限制我们使用super调用父类, 我们还是可以使用原始的方法来调用父类中的方法, 但是需要注意的是调用父类的方法要统一, 即全用super或全不用super, 而用super 的话, 调用的方式也最好是统一的

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python解析json实例方法
Nov 19 Python
基于Python log 的正确打开方式
Apr 28 Python
tensorflow 加载部分变量的实例讲解
Jul 27 Python
Python使用numpy模块实现矩阵和列表的连接操作方法
Jun 26 Python
django的ORM操作 删除和编辑实现详解
Jul 24 Python
python实现二分类的卡方分箱示例
Nov 22 Python
Python使用qrcode二维码库生成二维码方法详解
Feb 17 Python
Pytorch转tflite方式
May 25 Python
Python基于数列实现购物车程序过程详解
Jun 09 Python
详解用selenium来下载小姐姐图片并保存
Jan 26 Python
python3使用diagrams绘制架构图的步骤
Apr 08 Python
用Python提取PDF表格的方法
Apr 11 Python
Selenium控制浏览器常见操作示例
Aug 13 #Python
详解python3中的真值测试
Aug 13 #Python
利用Python将每日一句定时推送至微信的实现方法
Aug 13 #Python
Selenium鼠标与键盘事件常用操作方法示例
Aug 13 #Python
python删除字符串中指定字符的方法
Aug 13 #Python
Django contenttypes 框架详解(小结)
Aug 13 #Python
Python中的Numpy矩阵操作
Aug 12 #Python
You might like
提问的智慧
2006/10/09 PHP
PHP编程快速实现数组去重的方法详解
2017/07/22 PHP
Yii redis集合的基本使用教程
2020/06/14 PHP
js window.event对象详尽解析
2009/02/17 Javascript
JavaScript isPrototypeOf和hasOwnProperty使用区别
2010/03/04 Javascript
jquery如何把参数列严格转换成数组实现思路
2013/04/01 Javascript
深入document.write()与HTML4.01的非成对标签的详解
2013/05/08 Javascript
一个js导致的jquery失效问题的解决方法
2013/11/27 Javascript
JS实现鼠标点击展开或隐藏表格行的方法
2015/03/03 Javascript
jQuery获得document和window对象宽度和高度的方法
2015/03/25 Javascript
javascript中mouseover、mouseout使用详解
2015/07/19 Javascript
BootStrap轮播HTML代码(推荐)
2016/12/10 Javascript
jquery+ajax实现省市区三级联动 (封装和不封装两种方式)
2017/05/15 jQuery
vue加载完成后的回调函数方法
2018/09/07 Javascript
angularJs中ng-model-options设置数据同步的方法
2018/09/30 Javascript
详解VUE里子组件如何获取父组件动态变化的值
2018/12/26 Javascript
koa2+vue实现登陆及登录状态判断
2019/08/15 Javascript
微信小程序图片自适应实现解析
2020/01/21 Javascript
Vue 401配合Vuex防止多次弹框的案例
2020/11/11 Javascript
JavaScript仿京东轮播图效果
2021/02/25 Javascript
Python3.6基于正则实现的计算器示例【无优化简单注释版】
2018/06/14 Python
Flask web开发处理POST请求实现(登录案例)
2018/07/26 Python
TensorFlow Saver:保存和读取模型参数.ckpt实例
2020/02/10 Python
将keras的h5模型转换为tensorflow的pb模型操作
2020/05/25 Python
如何理解Python中包的引入
2020/05/29 Python
浅谈Python中的生成器和迭代器
2020/06/19 Python
如何完美的建立一个python项目
2020/10/09 Python
维也纳通行证:Vienna PASS
2019/07/18 全球购物
中专毕业自我鉴定
2013/10/16 职场文书
父亲的菜园教学反思
2014/02/13 职场文书
思想纪律作风整顿剖析材料
2014/10/11 职场文书
先进个人评语大全
2015/01/04 职场文书
公司经营目标责任书
2015/01/29 职场文书
转变工作作风心得体会
2016/01/23 职场文书
详解运行Python的神器Jupyter Notebook
2021/06/03 Python
服务器SVN搭建图文安装过程
2022/06/21 Servers