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实现下载网易云音乐的高清MV
Mar 16 Python
在Python中使用全局日志时需要注意的问题
May 06 Python
python+selenium打印当前页面的titl和url方法
Jun 22 Python
pytz格式化北京时间多出6分钟问题的解决方法
Jun 21 Python
Python BeautifulSoup [解决方法] TypeError: list indices must be integers or slices, not str
Aug 07 Python
简单介绍python封装的基本知识
Aug 10 Python
python中的TCP(传输控制协议)用法实例分析
Nov 15 Python
python Canny边缘检测算法的实现
Apr 24 Python
Python3批量创建Crowd用户并分配组
May 20 Python
python获取对象信息的实例详解
Jul 07 Python
Python+Selenium实现抖音、快手、B站、小红书、微视、百度好看视频、西瓜视频、微信视频号、搜狐视频、一点号、大风号、趣头条等短视频自动发布
Apr 13 Python
使用Python开发贪吃蛇游戏 SnakeGame
Apr 30 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
PHP自动选择 连接本地还是远程数据库
2010/12/02 PHP
解析smarty 截取字符串函数 truncate的用法介绍
2013/06/20 PHP
解析php中如何调用用户自定义函数
2013/08/06 PHP
php实现批量上传数据到数据库(.csv格式)的案例
2017/06/18 PHP
PHP实现的迪科斯彻(Dijkstra)最短路径算法实例
2017/09/16 PHP
thinkPHP框架动态配置用法实例分析
2018/06/14 PHP
JQuery从头学起第二讲
2010/07/04 Javascript
js用Date对象处理时间实现思路及代码
2013/01/31 Javascript
Knockout visible绑定使用方法
2013/11/15 Javascript
node.js实现逐行读取文件内容的代码
2014/06/27 Javascript
js实现字符串和数组之间相互转换操作
2016/01/12 Javascript
jQuery获取file控件中图片的宽高与大小
2016/08/04 Javascript
网络传输协议(http协议)
2016/11/18 Javascript
jquery 删除节点 添加节点 找兄弟节点的简单实现
2016/12/07 Javascript
从零学习node.js之express入门(六)
2017/02/25 Javascript
angularJS 发起$http.post和$http.get请求的实现方法
2017/05/18 Javascript
Angular 2.x学习教程之结构指令详解
2017/05/25 Javascript
Vue上传组件vue Simple Uploader的用法示例
2017/08/25 Javascript
关于Angularjs中跨域设置白名单问题
2018/04/17 Javascript
Windows系统下安装Python的SSH模块教程
2015/02/05 Python
python妙用之编码的转换详解
2017/04/21 Python
python的pdb调试命令的命令整理及实例
2017/07/12 Python
对python dataframe逻辑取值的方法详解
2019/01/30 Python
Python for循环与range函数的使用详解
2019/03/23 Python
Django 路由层URLconf的实现
2019/12/30 Python
基于TensorBoard中graph模块图结构分析
2020/02/15 Python
40行Python代码实现天气预报和每日鸡汤推送功能
2020/02/27 Python
用Python 爬取猫眼电影数据分析《无名之辈》
2020/07/24 Python
美国大尺码女装零售商:TORRID
2016/10/01 全球购物
彪马荷兰官网:PUMA荷兰
2019/05/08 全球购物
《夏夜多美》教学反思
2014/02/17 职场文书
人力资源作业细则
2014/03/03 职场文书
车贷收入证明范本
2014/09/14 职场文书
2014年检验科工作总结
2014/11/22 职场文书
委托函范文
2015/01/29 职场文书
2015年医德医风工作总结
2015/04/02 职场文书