我对python 继承中*args传入的约定感到困惑。super().__init__()我理解需要使用关键字参数**kwargs,以便必要时 CRO 中的类可以获取所需的参数,但为什么还有一个*args?
示例:假设 Sneaky 用作多重继承类结构的一部分,例如:
class Sneaky:
def __init__(self, sneaky = false, *args, **kwargs):
super().__init__(*args, **kwargs)
self.sneaky = sneaky
class Person:
def __init__(self, human = false, *args, **kwargs):
super().__init__(*args, **kwargs)
self.human = human
class Thief(Sneaky, Person):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
t = Thief(human = true, sneaky = true)
print(t.human)
# True
那么如果我们在下面删除 *args 呢?
class Sneaky:
def __init__(self, sneaky = false, **kwargs):
super().__init__( **kwargs)
self.sneaky = sneaky
class Person:
def __init__(self, human = false, **kwargs):
super().__init__(**kwargs)
self.human = human
class Thief(Sneaky, Person):
def __init__(self, **kwargs):
super().__init__( **kwargs)
t = Thief(human = true, sneaky = true)
print(t.human)
# True
翻阅古今
拉风的咖菲猫
相关分类