在多重继承的情况下超级参数

时间:2015-02-27 01:17:51

标签: python inheritance parameters multiple-inheritance super

我有一个继承自其他两个类的类,__init__同时接受这样的参数:

class A(object):
    def __init__(self, a):
        self.a = a

class B(object):
    def __init__(self, b):
        self.b = b

class C(A, B):
    def __init__(self, a, b):
        super(C, self).__init__(a, b)

c = ClassC(1, 2)

这会产生TypeError: __init__() takes exactly 2 arguments (3 given)

b中的B设置为固定值并仅将1个参数传递给super时,尝试访问b中的C会产生{{} 1}}:

AttributeError: 'ClassC' object has no attribute 'b'

手动调用class A(object): def __init__(self, a): self.a = a class B(object): def __init__(self, b): self.b = 2 class C(A, B): def __init__(self, a, b): super(C, self).__init__(a) print self.a print self.b c = ClassC(1, 2) 时,一切似乎都很好:

__init__

那么我怎样才能直接获得这种继承?如何在使用class A(object): def __init__(self, a): self.a = a class B(object): def __init__(self, b): self.b = b class C(A, B): def __init__(self, a, b): A.__init__(a) B.__init__(b) print self.a print self.b c = ClassC(1, 2) 时管理继承类的__init__的参数?它甚至可能吗? super如何知道哪些参数要传递给哪个类?

0 个答案:

没有答案