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 05 Python
Python易忽视知识点小结
May 25 Python
用pickle存储Python的原生对象方法
Apr 28 Python
python中map()函数的使用方法示例
Sep 29 Python
python使用生成器实现可迭代对象
Mar 20 Python
Python对多属性的重复数据去重实例
Apr 18 Python
简单谈谈python基本数据类型
Sep 26 Python
Python运行不显示DOS窗口的解决方法
Oct 22 Python
面向对象学习之pygame坦克大战
Sep 11 Python
python利用Excel读取和存储测试数据完成接口自动化教程
Apr 30 Python
python“静态”变量、实例变量与本地变量的声明示例
Nov 13 Python
解决Python字典查找报Keyerror的问题
May 26 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调用数据库的存贮过程
2006/10/09 PHP
PHP文件读写操作之文件写入代码
2011/01/13 PHP
php表单敏感字符过滤类
2014/12/08 PHP
ThinkPHP中url隐藏入口文件后接收alipay传值的方法
2014/12/09 PHP
javascript数组与php数组的地址传递及值传递用法实例
2015/01/22 PHP
php解析http获取的json字符串变量总是空白null
2015/03/02 PHP
php读取csv文件并输出的方法
2015/03/14 PHP
JS控制日期显示的小例子
2013/11/23 Javascript
JavaScript 学习笔记之变量及其作用域
2015/01/14 Javascript
avalon js实现仿微博拖动图片排序
2015/08/14 Javascript
JavaScript+Java实现HTML页面转为PDF文件保存的方法
2016/05/30 Javascript
AngularJS  自定义指令详解及实例代码
2016/09/14 Javascript
jquery事件与绑定事件
2017/03/16 Javascript
vuejs2.0子组件改变父组件的数据实例
2017/05/10 Javascript
深入理解AngularJs-scope的脏检查(一)
2017/06/19 Javascript
JS判断用户用的哪个浏览器实例详解
2018/10/09 Javascript
使用Javascript简单计算器
2018/11/17 Javascript
vue 搭建后台系统模块化开发详解
2019/05/01 Javascript
微信小程序实现用table显示数据库反馈的多条数据功能示例
2019/05/07 Javascript
Vue将props值实时传递 并可修改的操作
2020/08/09 Javascript
React Ant Design树形表格的复杂增删改操作
2020/11/02 Javascript
python中利用xml.dom模块解析xml的方法教程
2017/05/24 Python
对python mayavi三维绘图的实现详解
2019/01/08 Python
python3利用ctypes传入一个字符串类型的列表方法
2019/02/12 Python
django框架事务处理小结【ORM 事务及raw sql,customize sql 事务处理】
2019/06/27 Python
TensorFlow实现checkpoint文件转换为pb文件
2020/02/10 Python
Python unittest工作原理和使用过程解析
2020/02/24 Python
PyInstaller将Python文件打包为exe后如何反编译(破解源码)以及防止反编译
2020/04/15 Python
python3.7中安装paddleocr及paddlepaddle包的多种方法
2020/11/27 Python
python爬虫scrapy基于CrawlSpider类的全站数据爬取示例解析
2021/02/20 Python
孤独星球出版物:Lonely Planet Publications
2018/03/17 全球购物
Sephora丝芙兰印尼官方网站:购买化妆品和护肤品
2018/07/02 全球购物
Flesh Beauty官网:露华浓集团旗下彩妆品牌
2021/02/15 全球购物
介绍一下write命令
2012/09/24 面试题
2019年浪漫婚礼证婚词
2019/06/27 职场文书
python本地文件服务器实例教程
2021/05/02 Python