我有一个继承自其他两个类的类,__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
如何知道哪些参数要传递给哪个类?