温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Python中什么是内置函数super()

发布时间:2020-08-26 16:51:48 来源:亿速云 阅读:253 作者:Leah 栏目:编程语言

本篇文章给大家分享的是有关Python中什么是内置函数super(),小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

面向对象编程之super内置函数的用法

先来看一段代码:

定义一个名叫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就可以很好的解决这个问题了。

以上就是Python中什么是内置函数super(),小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI