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深入学习之对象的属性
Aug 31 Python
Python中使用第三方库xlutils来追加写入Excel文件示例
Apr 05 Python
Python文件和流(实例讲解)
Sep 12 Python
Python基于TCP实现会聊天的小机器人功能示例
Apr 09 Python
Python 对输入的数字进行排序的方法
Jun 23 Python
python爬虫获取小区经纬度以及结构化地址
Dec 30 Python
通过PYTHON来实现图像分割详解
Jun 26 Python
Python实现平行坐标图的绘制(plotly)方式
Nov 22 Python
Tensorflow矩阵运算实例(矩阵相乘,点乘,行/列累加)
Feb 05 Python
python3 通过 pybind11 使用Eigen加速代码的步骤详解
Dec 07 Python
Python3+PyCharm+Django+Django REST framework配置与简单开发教程
Feb 16 Python
使用Python脚本对GiteePages进行一键部署的使用说明
May 27 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处理json时中文问题的解决方法
2011/04/12 PHP
PHP中date()日期函数有关参数整理
2011/07/19 PHP
php+ajax做仿百度搜索下拉自动提示框(有实例)
2012/08/21 PHP
PHP资源管理框架Assetic简介
2014/06/12 PHP
PHP自带函数给数字或字符串自动补齐位数
2014/07/29 PHP
js parsefloat parseint 转换函数
2010/01/21 Javascript
jquery-easyui关闭tab自动切换到前一个tab
2010/07/29 Javascript
js 可拖动列表实现代码
2011/12/13 Javascript
JavaScript避免代码的重复执行经验技巧分享
2014/04/17 Javascript
JavaScript计算值然后把值嵌入到html中的实现方法
2016/10/29 Javascript
JavaScript自定义分页样式
2017/01/17 Javascript
angularjs的select使用及默认选中设置
2017/04/08 Javascript
vue之数据交互实例代码
2017/06/20 Javascript
浅谈Vue2.0父子组件间事件派发机制
2018/01/08 Javascript
使用vue中的v-for遍历二维数组的方法
2018/03/07 Javascript
vue服务端渲染缓存应用详解
2018/09/12 Javascript
Vue自定义指令上报Google Analytics事件统计的方法
2019/02/25 Javascript
[54:17]DOTA2-DPC中国联赛定级赛 RNG vs iG BO3第二场 1月10日
2021/03/11 DOTA
详解MySQL数据类型int(M)中M的含义
2016/11/20 Python
python购物车程序简单代码
2018/04/18 Python
一步步教你用python的scrapy编写一个爬虫
2019/04/17 Python
pytorch绘制并显示loss曲线和acc曲线,LeNet5识别图像准确率
2020/01/02 Python
python re的findall和finditer的区别详解
2020/11/15 Python
Pytorch1.5.1版本安装的方法步骤
2020/12/31 Python
requests在python中发送请求的实例讲解
2021/02/17 Python
巴西图书和电子产品购物网站:Saraiva
2017/06/07 全球购物
IdealFit官方网站:女性蛋白质、补充剂和运动服装
2019/03/24 全球购物
人力资源专业推荐信
2013/11/29 职场文书
国贸专业个人求职信范文
2014/01/08 职场文书
学期评语大全
2014/04/30 职场文书
服务标语大全
2014/06/18 职场文书
2015年元旦文艺晚会总结(学院)
2014/11/28 职场文书
煤矿安全生产工作总结
2015/08/13 职场文书
《游戏公平》教学反思
2016/02/20 职场文书
导游词之平津战役纪念馆
2019/11/04 职场文书
MySQL 自动填充 create_time 和 update_time
2022/05/20 MySQL