小编Kel*_*vin的帖子

与具有__init __()的super()有关的问题

鉴于以下3个课程,

class A(object):
    def __init__(self):
        print('A')
    def test(self):
        print('1')

class B(A):
    def __init__(self):
        super(B,self)        ## if .__init__() is not given here
        print('B')

class C(B, A):
    def __init__(self):
        super(C, self).__init__()
        print('C')
Run Code Online (Sandbox Code Playgroud)

如果我跑步D = C(),它将返回

B
C
Run Code Online (Sandbox Code Playgroud)

如果我跑步print(C.__mro__),它会给予帮助(<class '__main__.C'>, <class '__main__.B'>, <class '__main__.A'>, <class 'object'>)。我认为这意味着将在列表中执行A类,但是mro事实并非如此。

我想问为什么.__init__()A.__init__()发生这种情况。

python super

4
推荐指数
1
解决办法
87
查看次数

标签 统计

python ×1

super ×1