このコードで試したように、継承階層でargsとkwargsの両方を管理する方法はありますか? 「);」予想:Pythonの継承階層でargsとkwargsの両方を管理する
class Parent(object):
def __init__(self, motherArg1, motherArg2=100):
self.motherArg1 = motherArg1
self.motherArg2 = motherArg2
def printParent(self):
print self.motherArg1
print self.motherArg2
class Child(Parent):
def __init__(self, childArg1, *args, childArg2=100, **kwargs): # Doesn't work here
super(Child, self).__init__(*args, **kwargs)
self.childArg1 = childArg1
self.childArg2 = childArg2
def printChild(self):
print self.childArg1
print self.childArg2
child = Child(1, 3, childArg2=2, motherArg2=4)
child.printChild()
child.printParent()
構文は良くありません。私は1 2 3 4
を表示するようになっている
def __init__(self, childArg1, childArg2=100, *args, **kwargs)
は正しい構文ですが動作しません。
- 私はこの構文を試してみて
child = Child(1, childArg2=2, 3, motherArg2=4)
とき、私はでSyntaxErrorを取得:キーワード引数 - 後に非キーワード引数を私は
child = Child(1, 3, childArg2=2, motherArg2=4)
をしようとすると、私はTypeError例外を取得:__init __()はキーワード引数 'の複数の値を得ましたchildArg2'
適切に書式設定された*を質問に含めるには[編集]してください*。この場合、どの出力を得るのかははっきりしていますが、[mcve]は良い習慣です。 – jonrsharpe