在 child (multiple-inheritance) 实例中,我可以使用什么参数代替 `*args`?

what arg can I use instead of `*args` in child (multiple-inheritance) instance?

我可以在 child 实例中使用什么 arg 来代替 *args?例如在这段代码中:

class Foo:
    class_var = 'bar'
    def __init__(self):
        self.instance_var = 'baz'

class Test(Foo):
    def __init__(self, instance_var):
        Foo.__init__(self)
        self.new_instance = "duck"

child1 = Test(Foo) 
print("child1.instance_var: ", child1.instance_var)

输出为:

child1.instance_var:  baz

..但是当我创建下一个 child 时,脚本仅在我将 def __init__(self, instance_var): 更改为 def __init__(self, *args): 时才起作用。否则我会得到“TypeError: init() missing 1 required positional argument: 'instance_var'”.

此代码有效:

class Foo:
    class_var = 'bar'
    def __init__(self):
        self.instance_var = 'baz'

class Test(Foo):
    def __init__(self, *args):
        Foo.__init__(self)
        self.new_instance = "duck"

child1 = Test(Foo) 
print("child1.instance_var: ", child1.instance_var)

class Test1(Test):
    def __init__(self, *args):
        Test.__init__(self)
        self.new_instance = "new_duck"

child2 = Test1(Test)        
print("child2.instance_var: ", child2.new_instance)

输出为:

child1.instance_var:  baz
child2.instance_var:  new_duck

但是,这个没有:

class Foo:
    class_var = 'bar'
    def __init__(self):
        self.instance_var = 'baz'

class Test(Foo):
    def __init__(self, instance_var):
        Foo.__init__(self)
        self.new_instance = "duck"

child1 = Test(Foo) 
print("child1.instance_var: ", child1.instance_var)

class Test1(Test):
    def __init__(self, *args):
        Test.__init__(self)
        self.new_instance = "new_duck"

child2 = Test1(Test)        
print("child2.instance_var: ", child2.new_instance)

我不完全明白为什么(我应该把 instance_var 放在哪里,这样代码就不会抛出错误)。什么逻辑:循序渐进?

问题是我试图在

中声明instance_var
class Test(Foo):
    def __init__(self, instance_var):

该代码仅适用于 self,或适用于子 class 中的 self, *args

class Foo:
    class_var = 'bar'
    def __init__(self):
        self.instance_var = 'baz'

class Test(Foo):
    def __init__(self):
        Foo.__init__(self)
        self.new_instance = "duck"

child1 = Test() 
print("child1.instance_var: ", child1.instance_var)

class Test1(Test):
    def __init__(self):
        Test.__init__(self)
        self.new_instance = "new_duck"

child2 = Test1()        
print("child2.instance_var: ", child2.new_instance)

这解决了“TypeError: init() missing 1 required positional argument: 'instance_var'”的问题,当 *args 被移动时。