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通过shutil实现快速文件复制的方法
Mar 14 Python
详细解析Python中__init__()方法的高级应用
May 11 Python
Django中传递参数到URLconf的视图函数中的方法
Jul 18 Python
python实现给数组按片赋值的方法
Jul 28 Python
Python 运行 shell 获取输出结果的实例
Jan 07 Python
Python Pandas数据结构简单介绍
Jul 03 Python
Python字典中的值为列表或字典的构造实例
Dec 16 Python
Python实现银行账户资金交易管理系统
Jan 03 Python
Python新手如何理解循环加载模块
May 29 Python
python脚本第一行如何写
Aug 30 Python
Python中对象的比较操作==和is区别详析
Feb 12 Python
python基础之//、/与%的区别详解
Jun 10 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
在Windows版的PHP中使用ADO
2006/10/09 PHP
PHP 强制性文件下载功能的函数代码(任意文件格式)
2010/05/26 PHP
PHP实现四种基础排序算法的运行时间比较(推荐)
2016/08/11 PHP
Laravel手动分页实现方法详解
2016/10/09 PHP
jquery pagination插件实现无刷新分页代码
2009/10/13 Javascript
json属性名为什么要双引号(个人猜测)
2014/07/31 Javascript
JS显示表格内指定行html代码的方法
2015/03/31 Javascript
JavaScript之Object类型介绍
2015/04/01 Javascript
IE6-IE9使用JSON、table.innerHTML所引发的问题
2015/12/22 Javascript
AngularJS过滤器filter用法实例分析
2016/11/04 Javascript
JavaScript适配器模式详解
2017/10/19 Javascript
基于vue2.0实现仿百度前端分页效果附实现代码
2018/10/30 Javascript
node-red File读取好保存实例讲解
2019/09/11 Javascript
详解JavaScript中的Object.is()与&quot;===&quot;运算符总结
2020/06/17 Javascript
vue.js 解决v-model让select默认选中不生效的问题
2020/07/28 Javascript
基于javascript的无缝滚动动画1
2020/08/07 Javascript
Jquery+javascript实现支付网页数字键盘
2020/12/21 jQuery
vue watch监控对象的简单方法示例
2021/01/07 Vue.js
python 网络爬虫初级实现代码
2016/02/27 Python
python3+PyQt5实现使用剪贴板做复制与粘帖示例
2017/01/24 Python
python获取当前运行函数名称的方法实例代码
2017/04/06 Python
python的变量与赋值详细分析
2017/11/08 Python
selenium+python实现自动化登录的方法
2018/09/04 Python
详解Python给照片换底色(蓝底换红底)
2019/03/22 Python
Python如何操作office实现自动化及win32com.client的运用
2020/04/01 Python
几款主流好用的富文本编辑器(所见即所得常用编辑器)介绍
2021/03/17 Javascript
Tod’s英国官方网站:意大利奢华手工制作手袋和鞋履
2019/03/15 全球购物
大唐电信科技股份有限公司java工程师面试经历
2016/12/09 面试题
儿科护士自我鉴定
2013/10/14 职场文书
舞蹈教育学专业推荐信
2013/11/27 职场文书
幼儿园教学随笔感言
2014/02/23 职场文书
离婚协议书范本(2014版)
2014/09/28 职场文书
反腐倡廉剖析材料
2014/09/30 职场文书
初中生思想道德自我评价
2015/03/09 职场文书
关于环保的宣传稿
2015/07/23 职场文书
Python爬取某拍短视频
2021/06/11 Python