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使用面向对象方式创建线程实现12306售票系统
Dec 24 Python
Python用Pillow(PIL)进行简单的图像操作方法
Jul 07 Python
详解Python 模拟实现生产者消费者模式的实例
Aug 10 Python
pandas将DataFrame的列变成行索引的方法
Apr 10 Python
对tf.reduce_sum tensorflow维度上的操作详解
Jul 26 Python
python: 判断tuple、list、dict是否为空的方法
Oct 22 Python
Python获取网段内ping通IP的方法
Jan 31 Python
从0开始的Python学习016异常
Apr 08 Python
Pyqt清空某一个QTreeewidgetItem下的所有分支方法
Jun 17 Python
tensorflow 固定部分参数训练,只训练部分参数的实例
Jan 20 Python
Python爬虫爬取电影票房数据及图表展示操作示例
Mar 27 Python
python树莓派通过队列实现进程交互的程序分析
Jul 04 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 sprintf()函数让你的sql操作更安全
2008/07/23 PHP
谷歌音乐搜索栏的提示功能php修正代码
2011/05/09 PHP
PHP中使用SimpleXML检查XML文件结构实例
2015/01/07 PHP
PHP编程实现脚本异步执行的方法
2017/08/09 PHP
JavaScript版代码高亮
2006/06/26 Javascript
YUI 读码日记之 YAHOO.util.Dom - Part.1
2008/03/22 Javascript
jQuery html()等方法介绍
2009/11/18 Javascript
Javascript 正则表达式实现为数字添加千位分隔符
2015/03/10 Javascript
jquery实现多屏多图焦点图切换特效的方法
2015/05/04 Javascript
js 弹出对话框(遮罩)透明,可拖动的简单实例
2016/07/11 Javascript
iOS和Android用同一个二维码实现跳转下载链接的方法
2016/09/28 Javascript
js html css实现复选框全选与反选
2016/10/09 Javascript
JavaScript实现星星等级评价功能
2017/03/22 Javascript
详解自定义ajax支持跨域组件封装
2018/02/08 Javascript
详解vue 数据传递的方法
2018/04/19 Javascript
详解JS函数stack size计算方法
2018/06/18 Javascript
JS JQuery获取data-*属性值方法解析
2020/09/01 jQuery
vue 使用 v-model 双向绑定父子组件的值遇见的问题及解决方案
2021/03/01 Vue.js
[41:13]完美世界DOTA2联赛PWL S2 Forest vs Rebirth 第一场 11.20
2020/11/20 DOTA
numpy中矩阵合并的实例
2018/06/15 Python
Python TCPServer 多线程多客户端通信的实现
2019/12/31 Python
Pytorch之finetune使用详解
2020/01/18 Python
Django多数据库联用实现方法解析
2020/11/12 Python
CSS3实现3D翻书效果
2016/06/20 HTML / CSS
CSS3 transforms应用于背景图像的解决方法
2019/04/16 HTML / CSS
美国知名女性服饰品牌:New York & Company
2017/03/23 全球购物
英国专业美容产品在线:Mylee(从指甲到脱毛)
2020/07/06 全球购物
学校消防演习方案
2014/02/19 职场文书
厂办主管岗位职责范本
2014/02/28 职场文书
教师业务培训方案
2014/05/01 职场文书
项目建议书范文
2014/05/12 职场文书
承诺书的内容有哪些,怎么写?
2019/06/21 职场文书
这样写python注释让代码更加的优雅
2021/06/02 Python
redis cluster支持pipeline的实现思路
2021/06/23 Redis
SQL Server使用CROSS APPLY与OUTER APPLY实现连接查询
2022/05/25 SQL Server
linux目录管理方法介绍
2022/06/01 Servers