第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定

python類中super()和__init__()的區(qū)別

標(biāo)簽:
Python

最近有同学问我关于Python类中的super()和__init__()共同点和不同点的问题, 我今天把它们两个的异同点总结了一下,希望可以帮助遇到同样困惑的同学。


单继承时super()和__init__()实现的功能是类似的

class Base(object):
    def __init__(self):
        print 'Base create'
class childA(Base):
    def __init__(self):
        print 'creat A ',
        Base.__init__(self)
class childB(Base):
    def __init__(self):
        print 'creat B ',
        super(childB, self).__init__()
base = Base()
a = childA()
b = childB()

输出结果:

Base create
creat A  Base create
creat B  Base create

区别是使用super()继承时不用显式引用基类。


super()只能用于新式类中


把基类改为旧式类,即不继承任何基类

class Base():
    def __init__(self):
        print 'Base create'

执行时,在初始化b时就会报错:

super(childB, self).__init__()
TypeError: must be type, not classobj


super不是父类,而是继承顺序的下一个类


在多重继承时会涉及继承顺序,super()相当于返回继承顺序的下一个类,而不是父类,类似于这样的功能:

def super(class_name, self):
    mro = self.__class__.mro()
    return mro[mro.index(class_name) + 1]

mro()用来获得类的继承顺序。 例如:

class Base(object):
    def __init__(self):
        print 'Base create'
class childA(Base):
    def __init__(self):
        print 'enter A '
        # Base.__init__(self)
        super(childA, self).__init__()
        print 'leave A'
class childB(Base):
    def __init__(self):
        print 'enter B '
        # Base.__init__(self)
        super(childB, self).__init__()
        print 'leave B'
class childC(childA, childB):
    pass
c = childC()
print c.__class__.__mro__

输出结果如下:

enter A 
enter B 
Base create
leave B
leave A
(, , , , )

supder和父类没有关联,因此执行顺序是A —> B—>—>Base


执行过程相当于:初始化childC()时,先会去调用childA的构造方法中的 super(childA, self).__init__(), super(childA, self)返回当前类的继承顺序中childA后的一个类childB;然后再执行childB().__init__(),这样顺序执行下去。


在多重继承里,如果把childA()中的 super(childA, self).__init__() 换成Base._init_(self),在执行时,继承childA后就会直接跳到Base类里,而略过了childB:

enter A 
Base create
leave A
(, , , , )

从super()方法可以看出,super()的第一个参数可以是继承链中任意一个类的名字,


如果是本身就会依次继承下一个类;


如果是继承链里之前的类便会无限递归下去;


如果是继承链里之后的类便会忽略继承链汇总本身和传入类之间的类;


比如将childA()中的super改为:super(childC, self).__init__(),程序就会无限递归下去。 如:

  File "test.py", line 12, in __init__
    super(childC, self).__init__()
  File "test.py", line 12, in __init__
    super(childC, self).__init__()
  File "test.py", line 12, in __init__
    super(childC, self).__init__()
  File "test.py", line 12, in __init__
    super(childC, self).__init__()
  File "test.py", line 12, in __init__
    super(childC, self).__init__()
  File "test.py", line 12, in __init__
    super(childC, self).__init__()
  File "test.py", line 12, in __init__
    super(childC, self).__init__()
  File "test.py", line 12, in __init__
    super(childC, self).__init__()
  File "test.py", line 12, in __init__
    super(childC, self).__init__()
RuntimeError: maximum recursion depth exceeded while calling a Python object


super()可以避免重复调用


如果childA基础Base, childB继承childA和Base,如果childB需要调用Base的__init__()方法时,就会导致__init__()被执行两次:

class Base(object):
    def __init__(self):
        print 'Base create'
class childA(Base):
    def __init__(self):
        print 'enter A '
        Base.__init__(self)
        print 'leave A'
class childB(childA, Base):
    def __init__(self):
        childA.__init__(self)
        Base.__init__(self)
b = childB()

Base的__init__()方法被执行了两次

enter A 
Base create
leave A
Base create


使用super()是可避免重复调用

class Base(object):
    def __init__(self):
        print 'Base create'
class childA(Base):
    def __init__(self):
        print 'enter A '
        super(childA, self).__init__()
        print 'leave A'
class childB(childA, Base):
    def __init__(self):
        super(childB, self).__init__()
b = childB()
print b.__class__.mro()
enter A 
Base create
leave A
[, , , ]

原文来源:https://m.pythontab.com/article/1108


點(diǎn)擊查看更多內(nèi)容
TA 點(diǎn)贊

若覺(jué)得本文不錯(cuò),就分享一下吧!

評(píng)論

作者其他優(yōu)質(zhì)文章

正在加載中
  • 推薦
  • 評(píng)論
  • 收藏
  • 共同學(xué)習(xí),寫下你的評(píng)論
感謝您的支持,我會(huì)繼續(xù)努力的~
掃碼打賞,你說(shuō)多少就多少
贊賞金額會(huì)直接到老師賬戶
支付方式
打開微信掃一掃,即可進(jìn)行掃碼打賞哦
今天注冊(cè)有機(jī)會(huì)得

100積分直接送

付費(fèi)專欄免費(fèi)學(xué)

大額優(yōu)惠券免費(fèi)領(lǐng)

立即參與 放棄機(jī)會(huì)
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)

舉報(bào)

0/150
提交
取消