栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Python-super()的用法

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python-super()的用法

用法入门

super()就是用来调用父类(超类)的方式。

常用格式

super().method_name
super(type, object).method_name    #判断isinstance(object, type)

示例

class base():
    def __init__(self):
        print('---我是base类---')

    def run(self, catname):
        print('{},跑呀跑'.format(catname))

class Mytest(base):
    def __init__(self):
        print('---我是测试类---')
        super(Mytest, self).run('狸花猫')    #或者super().run('狸花猫')

mytest = Mytest()

-------------
输出:
---我是测试类---
狸花猫,跑呀跑

super(Mytest, self).run('xxx') 可以理解为:

super(Mytest, self).run('xxx') == base.__init__(self)

进阶解释 单继承时-super()和直接用父类类名调用方法的区别

单继承时,super()和直接用父类类名调用方法没有区别。

super()

class base():
    def __init__(self):
        print('---我是base类---')

class Mytest(base):
    def __init__(self):
        super().__init__()
        print('---我是测试类---')

mytest = Mytest()

--------------
输出:
---我是base类---
---我是测试类---

直接父类类名调用方法

class base():
    def __init__(self):
        print('---我是base类---')

class Mytest(base):
    def __init__(self):
        base.__init__(self)
        print('---我是测试类---')

mytest = Mytest()

----------------
输出:
---我是base类---
---我是测试类---

多继承时-super()和直接用父类类名调用方法的区别

super遵从广度优先搜索(MRO),直接父类类名调用方法则调用哪个父类就执行哪个父类。

super()

class A():
    def __init__(self):
        print('---我是A类---')

class B():
    def __init__(self):
        print('---我是B类---')

class Mytest(A, B):
    def __init__(self):
        print('---我是测试类---')
        super().__init__()

mytest = Mytest()

-----------
输出:
---我是测试类---
---我是A类---

直接父类类名调用方法

class A():
    def __init__(self):
        print('---我是A类---')

class B():
    def __init__(self):
        print('---我是B类---')

class Mytest(A, B):
    def __init__(self):
        B.__init__(self)
        A.__init__(self)
        print('---我是测试类---')

mytest = Mytest()

----------------
输出:
---我是B类---
---我是A类---
---我是测试类---

钻石继承时-super()和直接用父类类名调用方法的区别
  base
  /  
 /    
A      B
     /
    /
    C

    super()调用顺序:Mytest --> A --> B --> base

    直接父类类名调用方法调用顺序:Mytest --> A --> base --> B --> base

钻石继承时,如果不适用super函数,则base类会被多次调用,增大开销。

super()

class base():
    def __init__(self):
        print('---我是base类---')

class A(base):
    def __init__(self):
        print('---我是A类---')
        super().__init__()

class B(base):
    def __init__(self):
        print('---我是B类---')
        super().__init__()

class Mytest(A, B):
    def __init__(self):
        print('---我是测试类---')
        super().__init__()

mytest = Mytest()
----------------
输出:
---我是测试类---
---我是A类---
---我是B类---
---我是base类---

直接父类类名调用方法

class base():
    def __init__(self):
        print('---我是base类---')

class A(base):
    def __init__(self):
        print('---我是A类---')
        base.__init__(self)

class B(base):
    def __init__(self):
        print('---我是B类---')
        base.__init__(self)

class Mytest(A, B):
    def __init__(self):
        print('---我是测试类---')
        A.__init__(self)
        B.__init__(self)

mytest = Mytest()

----------------
输出:
---我是测试类---
---我是A类---
---我是base类---
---我是B类---
---我是base类---

super对比直接通过父类类名调用的优势
    使用super时,如果类的继承关系发生改变了,只需要修改class定义时的继承关系;而如果使用父类类名直接调用,则需要将类中所有用到父类类名的地方全都替换。Python3中super遵循MRO-广度优先原则,且父类只调用一次,避免重复调用。
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/701039.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号