Python super() 函数 and 构造方法

本文深入探讨了Python中super()函数的使用方法,解释了其在解决多重继承问题中的作用,包括MRO(方法解析顺序)的原理。通过具体示例展示了如何在子类构造函数中调用父类构造函数,避免了属性的重复初始化。
部署运行你感兴趣的模型镜像

描述

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

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

MRO 就是类的方法解析顺序表, 其实也就是继承父类方法时的顺序表。

语法

super(type[, object-or-type])
type – 类。
object-or-type – 类,一般是 self

Python3.x 和 Python2.x 的一个区别是: Python 3 可以使用直接使用 super().xxx 代替 super(Class, self).xxx
super().add(x)
super(B, self).add(x)

other: 构造方法

>    __init__()

初始化新创建对象的状态,在一个对象被创建以后会立即调用。(即初始化)

构造方法中的初始值无法继承

class Bird:
    def __init__(self):
          self.hungry = True
    def eat(self):
          if self.hungry:
               print ('Ahahahah')
          else:
               print ('No thanks!')

class SongBird(Bird):
     def __init__(self):
          self.sound = 'Squawk'
     def sing(self):
          print (self.sound)

sb = SongBird()
sb.sing()    # 能正常输出
sb.eat()     # 报错,因为 songgird 中没有 hungry 特性
解决办法:使用super函数
class SongBird(Bird):
     def __init__(self):
          super().__init__()
          self.sound = 'Squawk'
     def sing(self):
          print (self.sound)
使用 super() 可以很好地避免构造函数被调用两次。
class A():
    def __init__(self):
        print('enter A')
        print('leave A')


class B(A):
    def __init__(self):
        print('enter B')
        super().__init__()
        print('leave B')


class C(A):
    def __init__(self):
        print('enter C')
        super().__init__()
        print('leave C')


class D(B, C):
    def __init__(self):
        print('enter D')
        super().__init__()
        print('leave D')


d = D()
'''
enter D
enter B
enter C
enter A
leave A
leave C
leave B
leave D
'''

您可能感兴趣的与本文相关的镜像

Python3.8

Python3.8

Conda
Python

Python 是一种高级、解释型、通用的编程语言,以其简洁易读的语法而闻名,适用于广泛的应用,包括Web开发、数据分析、人工智能和自动化脚本

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值