python super用法及原理详解


Posted in Python onJanuary 20, 2020

这篇文章主要介绍了python super用法及原理详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

概念

super作为python的内建函数。主要作用如下:

  • 允许我们避免使用基类
  • 跟随多重继承来使用

实例

在单个继承的场景下,一般使用super来调用基类来实现:
下面是一个例子:

class Mammal(object):
 def __init__(self, mammalName):
  print(mammalName, 'is a warm-blooded animal.')
  
class Dog(Mammal):
 def __init__(self):
  print('Dog has four legs.')
  super().__init__('Dog')
  
d1 = Dog()

输出结果:

➜ super git:(master) ✗ py super_script.py

Dog has four legs.

Dog is a warm-blooded animal.

super在多重继承里面的使用:

下面是一个例子:

class Animal:
 def __init__(self, animalName):
  print(animalName, 'is an animal.');
class Mammal(Animal):
 def __init__(self, mammalName):
  print(mammalName, 'is a warm-blooded animal.')
  super().__init__(mammalName)

class NonWingedMammal(Mammal):
 def __init__(self, NonWingedMammalName):
  print(NonWingedMammalName, "can't fly.")
  super().__init__(NonWingedMammalName)
class NonMarineMammal(Mammal):
 def __init__(self, NonMarineMammalName):
  print(NonMarineMammalName, "can't swim.")
  super().__init__(NonMarineMammalName)
class Dog(NonMarineMammal, NonWingedMammal):
 def __init__(self):
  print('Dog has 4 legs.');
  super().__init__('Dog')

d = Dog()
print('')
bat = NonMarineMammal('Bat')

输出结果:

➜ super git:(master) ✗ py super_muli.py
Dog has 4 legs.
Dog can't swim.
Dog can't fly.
Dog is a warm-blooded animal.
Dog is an animal.

Bat can't swim.
Bat is a warm-blooded animal.
Bat is an animal.

参考文档

https://www.programiz.com/python-programming/methods/built-in/super

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

Python 相关文章推荐
利用python获得时间的实例说明
Mar 25 Python
python清除字符串里非字母字符的方法
Jul 02 Python
Python 两个列表的差集、并集和交集实现代码
Sep 21 Python
Python3 加密(hashlib和hmac)模块的实现
Nov 23 Python
Flask框架响应、调度方法和蓝图操作实例分析
Jul 24 Python
Python实现计算文件MD5和SHA1的方法示例
Jun 11 Python
python GUI库图形界面开发之PyQt5状态栏控件QStatusBar详细使用方法实例
Feb 28 Python
python如何输出反斜杠
Jun 18 Python
python中append函数用法讲解
Dec 11 Python
详解Python调用系统命令的六种方法
Jan 28 Python
Python实现曲线拟合的最小二乘法
Feb 19 Python
Python实现位图分割的效果
Nov 20 Python
tensorflow 变长序列存储实例
Jan 20 #Python
在tensorflow中实现去除不足一个batch的数据
Jan 20 #Python
Tensorflow实现在训练好的模型上进行测试
Jan 20 #Python
Python线程条件变量Condition原理解析
Jan 20 #Python
tensorflow tf.train.batch之数据批量读取方式
Jan 20 #Python
Python list运算操作代码实例解析
Jan 20 #Python
Python模块future用法原理详解
Jan 20 #Python
You might like
自制短波长线天线频率预选器 - 成功消除B2K之流的镜像
2021/03/02 无线电
用PHP连mysql和oracle数据库性能比较
2006/10/09 PHP
在任意字符集下正常显示网页的方法一
2007/04/01 PHP
PHP在字符断点处截断文字的实现代码
2011/04/21 PHP
php延迟静态绑定实例分析
2015/02/08 PHP
checkbox 复选框不能为空
2009/07/11 Javascript
jQuery入门第一课 jQuery选择符
2010/03/14 Javascript
jQuery中jqGrid分页实现代码
2011/11/04 Javascript
js confirm()方法的使用方法实例
2013/07/13 Javascript
JQuery加载图片自适应固定大小的DIV
2013/09/12 Javascript
angularjs实现与服务器交互分享
2014/06/24 Javascript
AngularJS实现元素显示和隐藏的几个案例
2015/12/09 Javascript
knockoutjs动态加载外部的file作为component中的template数据源的实现方法
2016/09/01 Javascript
jquery二级目录选中当前页的css样式
2016/12/08 Javascript
如何将你的AngularJS1.x应用迁移至React的方法
2018/02/01 Javascript
「中高级前端面试」JavaScript手写代码无敌秘籍(推荐)
2019/04/08 Javascript
vue组件中watch props根据v-if动态判断并挂载DOM的问题
2019/05/12 Javascript
layui 数据表格+分页+搜索+checkbox+缓存选中项数据的方法
2019/09/21 Javascript
python搭建简易服务器分析与实现
2012/12/15 Python
python实现按行切分文本文件的方法
2016/04/18 Python
Python中的defaultdict与__missing__()使用介绍
2018/02/03 Python
Django中的静态文件管理过程解析
2019/08/01 Python
python getpass实现密文实例详解
2019/09/24 Python
python使用openCV遍历文件夹里所有视频文件并保存成图片
2020/01/14 Python
浅谈python累加求和+奇偶数求和_break_continue
2020/02/25 Python
Python发送邮件封装实现过程详解
2020/05/09 Python
Python中使用Selenium环境安装的方法步骤
2021/02/22 Python
CSS3实现红包抖动效果
2020/12/23 HTML / CSS
雅诗兰黛美国官网:Estee Lauder美国
2016/07/21 全球购物
白俄罗斯女装和针织品网上商店:Presli.by
2019/10/13 全球购物
N:Philanthropy官网:美国洛杉矶基础款服装
2020/06/09 全球购物
材料员岗位职责
2014/03/13 职场文书
拿破仑传读书笔记
2015/07/01 职场文书
大学生受助感言
2015/08/01 职场文书
课改心得体会范文
2016/01/25 职场文书
MySQL修炼之联结与集合浅析
2021/10/05 MySQL