先来看一段代码:

定义一个名叫People的父类,又定义了一个叫Teacher的老师类和一个叫Student的学生类

来继承People的类,并根据这两个子类实例化出两个对象s1和t1。

class Date:
def __init__(self,year,mon,day):
self.year=year
self.mon=mon
self.day=day def birth_info(self):
print("The birth is %s-%s-%s"%(self.year,self.mon,self.day)) class People:
def __init__(self,name,age):
self.name=name
self.age=age def walk(self):
print("%s is walking"%self.name) class Teacher(People):
def __init__(self,name,age,year,mon,day,course):
People.__init__(self,name,age)
self.course=course
self.birth=Date(year,mon,day) def teach(self):
print("%s is teaching"%self.name) class Student(People):
def __init__(self,name,age,year,mon,day,group):
People.__init__(self,name,age)
self.birth = Date(year, mon, day)
self.group=group def study(self):
print("%s is studying"%self.name)
t1=Teacher("alex",28,1989,9,2,"python")
s1=Student("jack",22,1995,2,8,"group2")

现在问题来了,假如因为需要,我要修改老师类和学生类的父类People的名字。

这样一来,在老师类Teacher和学生类Student中继承的类People也要修改,以及它们

调用的init方法的那个父类也要修改名字,太麻烦了有没有?

这时候就可以使用super()这个内置函数来搞定了。

在python解释器中查看帮助信息:

help(super)

得到如下信息:

Help on class super in module builtins:

class super(object)
| super() -> same as super(__class__, <first argument>)
| super(type) -> unbound super object
| super(type, obj) -> bound super object; requires isinstance(obj, type)
| super(type, type2) -> bound super object; requires issubclass(type2, type)

super是一个内置函数,加括号就得到一个对象,对象super()加"."可以直接调用父类的init方法。

这个对象在调用父类的init时,实际上就是在调用父类的绑定方法,所以就不需要在括号里加上self了。

修改后的代码如下:

class Date:
def __init__(self,year,mon,day):
self.year=year
self.mon=mon
self.day=day def birth_info(self):
print("The birth is %s-%s-%s"%(self.year,self.mon,self.day)) class People:
def __init__(self,name,age):
self.name=name
self.age=age def walk(self):
print("%s is walking"%self.name) class Teacher(People):
def __init__(self,name,age,year,mon,day,course):
super().__init__(name,age)
self.course=course
self.birth=Date(year,mon,day) def teach(self):
print("%s is teaching"%self.name) class Student(People):
def __init__(self,name,age,year,mon,day,group):
super().__init__(name,age)
self.birth = Date(year, mon, day)
self.group=group def study(self):
print("%s is studying"%self.name)
t1=Teacher("alex",28,1989,9,2,"python")
s1=Student("jack",22,1995,2,8,"group2")

这样一来,父类的名字改变了,代码里面继承的父类的init方法的名字也不需要修改了。

python2中,也可以使用super,其调用方法为:super(Teacher,self)

使用super()函数时,python会在mro列表中继续搜索下一个类。

只要每个重定义的方法统一使用super()并只调用它一次,那么控制流最终会遍历完整个mro列表。每个方法只会调用一次。
使用super调用的所有的属性,都是从mro列表当前的位置往后找,看mro列表的顺序就可以看到子类的继承关系

查看上面代码中Teacher这个子类的mro列表可以使用这个方法:

Teacher.mro()

使用super可以避免使用多重继承时,子类继承父类的顺序问题。

子类继承父类的数据属性和函数属性时,先执行的先生效,当后面的代码与前面的代码有冲突时,

后面的代码会把前面的代码覆盖掉,不使用super时需要自己解决继承的顺序问题,使用super就可以很好的解决这个问题了。

面向对象编程之super内置函数的用法的更多相关文章

  1. freemarker内置函数和用法

    原文链接:http://www.iteye.com/topic/908500 在我们应用Freemarker 过程中,经常会操作例如字符串,数字,集合等,却不清楚Freemrker 有没有类似于Jav ...

  2. [SQL]SUTFF内置函数的用法 (删除指定长度的字符并在指定的起始点插入另一组字符)

    STUFF 删除指定长度的字符并在指定的起始点插入另一组字符. 语法 STUFF ( character_expression , start , length , character_express ...

  3. enumerate next eval reload 内置函数的用法

    enumerate next eval reload 内置函数的用法 #enumerate() 函数用于将一个可遍历的数据对象(如列表.元组或字符串)组合为一个索引序列,同时列出数据和数据下标,一般用 ...

  4. Python内置函数reversed()用法分析

    Python内置函数reversed()用法分析 这篇文章主要介绍了Python内置函数reversed()用法,结合实例形式分析了reversed()函数的功能及针对序列元素相关操作技巧与使用注意事 ...

  5. $Python常用内置函数典型用法

    Python中有许多功能丰富的内置函数,本文基于Python 2.7,就常用的一些函数的典型用法做一些积累,不断更新中. sorted函数的三种用法 # coding:utf-8 # sorted函数 ...

  6. 面向对象super内置函数(转)

    super函数用来解决钻石继承. 一.python的继承以及调用父类成员 父类: class Base(object): def __init__(self): print("base in ...

  7. [SQL]SUTFF内置函数的用法

    STUFF 删除指定长度的字符并在指定的起始点插入另一组字符. 语法 STUFF ( character_expression , start , length , character_express ...

  8. python内置函数getattr用法

    class Tests(object):    #定义类     aaa = '10'          #定义变量       def test(self):     #定义类的方法test     ...

  9. Freemarker 内置函数 数字、字符串、日期格式化用法介绍

    在用FreeMarker过程中,感觉FreeMarker的字符串,日期,集合等处理能力还是很强大的,上网搜了一些资料,整理如下,以便能帮助大家更熟练的应用Freemarker完成项目开发. 一.Seq ...

随机推荐

  1. POJ 1741 Tree(树的点分治,入门题)

    Tree Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 21357   Accepted: 7006 Description ...

  2. [51nod Round 15 B ] 完美消除

    数位DP. 比较蛋疼的是,设a[i]表示第i位上数字,比方说a[1]<a[2]>a[3],且a[1]==a[3]时,这两位上的数可以放在一起搞掉. 所以就在正常的f数组里多开一维,表示后面 ...

  3. hdu_2670Girl Love Value(dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2670 Girl Love Value Time Limit: 2000/1000 MS (Java/O ...

  4. Java大数应用

    1.大数加法 import java.math.BigInteger; import java.util.Scanner; public class Main { public static void ...

  5. C#、.NET Framework、CLR的关系

    很多人没有将C#..NET Framework(.NET框架).CLR(Common Language Runtime,公共语言运行库)这三者之间的关系区分清楚,认为其版本号是一一对应的.其实不然,. ...

  6. html5 文本格式化

    通常标签 <strong> 替换加粗标签 <b> 来使用, <em> 替换 <i>标签使用.然而,这些标签的含义是不同的:<b> 与< ...

  7. Spark算子--union、intersection、subtract

    转载请标明出处http://www.cnblogs.com/haozhengfei/p/252bcc1d1ab30c430d347279d5827615.html union.intersection ...

  8. include指令与include动作的区别(面试要考)

    include指令: 语法格式:<%@ include file=" " ...%> 发生作用的时间:页面转换期间 包含的内容:页面的实际内容 转换成的servlet: ...

  9. mysql数据库创建、删除数据库

    一.创建数据库(默认字符集和排序规则)     (1)创建数据库 mysql> CREATE DATABASE my_db1; Query OK, 1 row affected (0.00 se ...

  10. PHP 正则表达式匹配函数 preg_match 与 preg_match_all

    preg_match() preg_match() 函数用于进行正则表达式匹配,成功返回 1 ,否则返回 0 . 语法: 1 int preg_match( string pattern, strin ...