我只是潜入一些更先进的蟒蛇科目(至少先进给我)。我现在正在阅读有关多重继承,以及如何使用super()。我或多或少的了解超级功能的使用方式,但(1)这样做是怎么回事?
class First(object):
def __init__(self):
print "first"
class Second(object):
def __init__(self):
print "second"
class Third(First, Second):
def __init__(self):
First.__init__(self)
Second.__init__(self)
print "that's it"
usage of super() will also be correct when the Derived class inherits
from multiple base classes and some or all of them have init
methods
所以我重写了上面的例子如下:
class First(object):
def __init__(self):
print "first"
class Second(object):
def __init__(self):
print "second"
class Third(First, Second):
def __init__(self):
super(Third, self).__init__(self)
print "that's it"
然而,这只能运行第一个可以找到的init,这是在First中。 (2)可以使用super()来运行来自第一和第二的init,如果是这样,怎么办?运行super(Third,self).__ init __(self)两次只运行First.init()两次..
增加一些混乱。如果继承的类的init()函数采用不同的参数怎么办?例如,如果我有这样的话,该怎么办?
class First(object):
def __init__(self, x):
print "first"
class Second(object):
def __init__(self, y, z):
print "second"
class Third(First, Second):
def __init__(self, x, y, z):
First.__init__(self, x)
Second.__init__(self, y, z)
print "that's it"
(3)如何使用super()将相关参数提供给不同的继承类init函数?
欢迎所有技巧!
PS。由于我有几个问题,我让他们大胆和编号他们..