Python multiple inheritance constructor not called when using super()

前端 未结 2 713
我寻月下人不归
我寻月下人不归 2021-01-13 06:54

Consider the following code:

class A(object):
    def __init__(self):
        pass
class B(object):
    def __init__(self):
        self.something = \'blue\'         


        
2条回答
  •  囚心锁ツ
    2021-01-13 07:20

    Superclasses should use super if their subclasses do. If you add the super().__init__() line into A and B your example should work again.

    Check the method resolution order of C:

    >>> C.mro()
    [__main__.C, __main__.A, __main__.B, builtins.object]
    

    This article should clear things up.

提交回复
热议问题