Calling superclass constructors in python with different arguments

后端 未结 2 1258
伪装坚强ぢ
伪装坚强ぢ 2020-11-30 11:19
class A():
    def __init__( self, x, y):
        self.x = x
        self.y = y

class B():
    def __init__( self, z=0):
        self.z = z  

class AB(A,B):
    de         


        
2条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-30 11:36

    You didn't pass self.

    class AB(A, B):
        def __init__(self, x, y, z=0):
            A.__init__(self, x, y)
            B.__init__(self, z)
    

    Note that if this inheritance hierarchy gets more complicated, you'll run into problems with constructors not executing or getting reexecuted. Look into super (and the problems with super), and don't forget to inherit from object if you're on 2.x and your class doesn't inherit from anything else.

提交回复
热议问题