如何从基类中调用父类__init__? [重复]

问题描述 投票:0回答:1
class A(object):
    def __init__(self, id):
        print("in A")


class B(object):
    def __init__(self, id1, id2):
        print("In B")

class C(A,B):
    def __init__(self, id1, id2):
        super(C, self).__init__(id1)
        super(C,self).__init__(id1,id2)

我将C的对象称为C(1,2)。

它抛出错误:

TypeError: __init__() takes exactly 2 arguments (3 given)

我可以知道如何从C的__init__调用父类'__init__

python oop multiple-inheritance init
1个回答
-1
投票

试试这个:

class C(A, B):
    def __init__(self, id1, id2):
        A.__init__(self, id1)
        B.__init__(self, id1, id2)
© www.soinside.com 2019 - 2024. All rights reserved.