概念:

  super() 函数是用于调用父类(超类)的一个方法。

super 是用来解决多重继承问题的,直接用类名调用父类方法在使用单继承的时候没问题,但是如果使用多继承,会涉及到查找顺序(MRO)、重复调用(钻石继承)等种种问题。

 格式:

super(type[, object-or-type])
  • type -- 类。
  • object-or-type -- 类,一般是 self
  • Python3.x 和 Python2.x 的一个区别是: Python 3 可以使用直接使用 super().xxx 代替 super(Class, self).xxx :

  简单实例:

  可以说明supper的概念和使用方法.

class Base(object):
def __init__(self):
print "Base created" class ChildA(Base):
def __init__(self):
Base.__init__(self) class ChildB(Base):
def __init__(self):
super(ChildB, self).__init__() ChildA()
ChildB()

  输出结果:

Base created
Base created

  高级实例:

#!/usr/bin/python
# -*- coding: UTF-8 -*- class FooParent(object):
def __init__(self):
self.parent = 'I\'m the parent.'
print ('Parent') def bar(self,message):
print ("%s from Parent" % message) class FooChild(FooParent):
def __init__(self):
# super(FooChild,self) 首先找到 FooChild 的父类(就是类 FooParent),然后把类B的对象 FooChild 转换为类 FooParent 的对象
super(FooChild,self).__init__()
print ('Child') def bar(self,message):
super(FooChild, self).bar(message)
print ('Child bar fuction')
print (self.parent) if __name__ == '__main__':
fooChild = FooChild()
fooChild.bar('HelloWorld')

  输出结果:

Parent
Child
HelloWorld from Parent
Child bar fuction
I'm the parent.

Python super() 函数的概念和例子的更多相关文章

  1. Python super() 函数

    super() 函数是用于调用父类(超类)的一个方法. super 是用来解决多重继承问题的,直接用类名调用父类方法在使用单继承的时候没问题,但是如果重定义某个方法,该方法会覆盖父类的同名方法,但有时 ...

  2. python super()函数详解

    引言: 在类的多继承使用场景中,重写父类的方法时,可能会考虑到需要重新调用父类的方法,所以super()函数就是比较使用也很必要的解决方法: 文章来源: http://www.cnblogs.com/ ...

  3. python super()函数:调用父类的构造方法

    python子类会继承父类所有的类属性和类方法.严格来说,类的构造方法其实就是实例方法,因此,父类的构造方法,子类同样会继承. 我们知道,python是一门支持多继承的面向对象编程语言,如果子类继承的 ...

  4. Python setattr() 函数 ,Python super() 函数: Python 内置函数 Python 内置函数

    描述 setattr 函数对应函数 getatt(),用于设置属性值,该属性必须存在. 语法 setattr 语法: setattr(object, name, value) 参数 object -- ...

  5. Python面试题之Super函数

    这是个高大上的函数,在python装13手册里面介绍过多使用可显得自己是高手 23333. 但其实他还是很重要的. 简单说, super函数是调用下一个父类(超类)并返回该父类实例的方法. 这里的下一 ...

  6. 由Python的super()函数想到的

    python-super *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !im ...

  7. python学习之路---day20--面向对象--多继承和super() 函数

    一:python多继承 python多继承中,当一个类继承了多个父类时候,这个类拥有多个父类的所欲非私有的属性 l例子: class A: pass class B(A): pass class C( ...

  8. python函数纯概念汇总(一)

    在使用python的时候由于前期基本概念没有分清楚,所以需要重新归纳汇总学一学. 一.什么是函数: 函数一词来源于数学,但编程中的「函数」概念,与数学中的函数是有很大不同的,编程中的函数在英文中也有很 ...

  9. 认识python中的super函数

    需求分析 在类继承中,存在这么一种情况: class Human(object): def Move(self): print("我会走路...") class Man(Human ...

随机推荐

  1. 刷机魅蓝note2

    直接上了. 情况: 魅蓝note2 公共版  忘记flyme 密码,手机号也换了,,找不回了..只能刷了.网上说就算刷了也不行,还是要输入flyme 密码 1. 直接刷官方的zip 包,不行,仍然要输 ...

  2. map获取数字与int比较

    已知 map.get("id")为数字,如:123问题 id.equals(123) 结果为false而使用 int id = (Integer)map.get("id& ...

  3. C++ STL库的总结以及实现原理

    STL的容器可以分为以下几个大类:一:序列容器, 有vector, list, deque, string. 二 : 关联容器,     有set, multiset, map, mulmap has ...

  4. Python从入门到精通之Third!

    Python运算符 算数运算符:+    -    *   /     %    //    **  比较运算符:==    >     <     >=   大于等于      & ...

  5. Linux 第十六天

    十六.服务管理 1.服务分类 1)RPM包默认安装的服务:包括独立的服务.基于xinetd的服务 2)源码包安装的服务 3)RPM安装服务和源码包安装服务的区别就是安装位置的不同 >源码包安装在 ...

  6. appium:运行脚本时,报404的解决办法

    对于报404的错,不要怀疑,在环境正常的情况下,一定是你的端口被占用了. 就用:查看端口:netstat -aon|findstr 5037 查看进程:tasklist /fi "PID e ...

  7. LOJ-10091(强连通分量)

    题目链接:传送门 思路: 多少头牛收到所有牛头牛的喜欢,喜欢具有传递性,所以将互相喜欢的牛视为一个点,就是有向图的 缩点,收到所有牛的喜欢要求这个“点”没有出度,所以缩点之后统计所有没有出度的点就是结 ...

  8. mybatis-plus 3.X 配置

    官网配置参数说明地址:https://mp.baomidou.com/config/#logicdeletevalue 本地配置:yml mybatis-plus: mapper-locations: ...

  9. EFLinq查询

    1.无参数查询var model = db.Database.SqlQuery<UserInfo>("select* from UserInfoes ").ToList ...

  10. input checkbox复选框取值

    <table> <!--列表表头 开始 --> <tr class="ui-widget ui-state-hover" style="he ...