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 相关文章推荐
使用XML库的方式,实现RPC通信的方法(推荐)
Jun 14 Python
python如何统计序列中元素
Jul 31 Python
关于Tensorflow中的tf.train.batch函数的使用
Apr 24 Python
浅谈python脚本设置运行参数的方法
Dec 03 Python
pip安装py_zipkin时提示的SSL问题对应
Dec 29 Python
Python判断两个文件是否相同与两个文本进行相同项筛选的方法
Mar 01 Python
python实现二维数组的对角线遍历
Mar 02 Python
python各类经纬度转换的实例代码
Aug 08 Python
Python打印特殊符号及对应编码解析
May 07 Python
Tensorflow tf.tile()的用法实例分析
May 22 Python
windows安装python超详细图文教程
May 21 Python
Python中Numpy和Matplotlib的基本使用指南
Nov 02 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/01/05 PHP
PHP strstr 函数判断字符串是否否存在的实例代码
2013/09/28 PHP
php汉字转拼音的示例
2014/02/27 PHP
使用PHP如何实现高效安全的ftp服务器(一)
2015/12/20 PHP
CodeIgniter读写分离实现方法详解
2016/01/20 PHP
PHP实现限制域名访问的实现代码(本地验证)
2020/09/13 PHP
让div层随鼠标移动的实现代码 ie ff
2009/12/18 Javascript
JavaScript 学习笔记(十五)
2010/01/28 Javascript
JS正则验证邮箱的格式详细介绍
2013/11/19 Javascript
jQuery动态显示和隐藏datagrid中的某一列的方法
2013/12/11 Javascript
Javascript数据结构与算法之列表详解
2015/03/12 Javascript
javascript判断数组内是否重复的方法
2015/04/21 Javascript
jQuery+PHP星级评分实现方法
2015/10/02 Javascript
Angular2 路由问题修复详解
2017/03/01 Javascript
JS实现DOM节点插入操作之子节点与兄弟节点插入操作示例
2018/07/30 Javascript
当vue路由变化时,改变导航栏的样式方法
2018/08/22 Javascript
vue下canvas裁剪图片实例讲解
2020/04/16 Javascript
详解Python Socket网络编程
2016/01/05 Python
微信跳一跳python辅助脚本(总结)
2018/01/11 Python
python 读文件,然后转化为矩阵的实例
2018/04/23 Python
Python实现的当前时间多加一天、一小时、一分钟操作示例
2018/05/21 Python
python查询文件夹下excel的sheet名代码实例
2019/04/02 Python
django celery redis使用具体实践
2019/04/08 Python
Python利用sqlacodegen自动生成ORM实体类示例
2019/06/04 Python
Python OpenCV实现鼠标画框效果
2020/08/19 Python
python asyncio 协程库的使用
2021/01/21 Python
移动端适配 使px自动转换rem
2019/08/26 HTML / CSS
Cotton On南非:澳洲时尚平价品牌
2018/06/28 全球购物
来自南加州灵感的工作和娱乐服装:TravisMathew
2019/05/01 全球购物
乌克兰排名第一的在线旅游超市:Farvater.Travel
2020/01/02 全球购物
银行实习的自我鉴定
2013/12/10 职场文书
报关报检委托书
2014/04/08 职场文书
年终奖发放方案
2014/06/02 职场文书
幼师小班个人总结
2015/02/12 职场文书
导游词之新疆尼雅遗址
2019/10/16 职场文书
Python中的嵌套循环详情
2022/03/23 Python