继承:将父类__init__ args放在子类的创建中

时间:2017-02-19 18:24:47

标签: python

当我实例化一个Child类时,如何为Parent类提供其args?我是否需要首先使用其args实例化Parent类,然后在我的Child args中使用该对象?

class Parent(object):
    def __init__(self, args):
        self.args = args

class Child(Parent):
    def __init__(self):
        super().__init__()

object = Child()

我在哪里放置父类args?

1 个答案:

答案 0 :(得分:1)

您只需将其传递给父母的__init__电话。

更改Child以接受父级的参数,然后在__init__函数中传递它们。

class Child(Parent):
    def __init__(self, args):
        super().__init__(args)