私はスーパークラスとサブクラスを持っています。パイソン - オーバーライド親クラスの引数
class Vehicle:
def __init__(self, new_fuel, new_position):
self.fuel = new_fuel
self.position = new_position
class Car(Vehicle):
# Here, I am stating that when Car is initialized, the position will be
# at (0, 0), so when you call it, you do not have to give it a new_position argument
def __init__(self, new_fuel, new_position=(0, 0)):
super(Car, self).__init__(new_fuel, new_position)
self.new_position = new_position
問題:
私はこれが(0、0) の10燃料と位置でCarオブジェクトを初期化したいが、私はnew_positionの引数に入れたくないので、私すべての車が初期化されるとき、それらの位置は(0、0)であると述べました。また、親クラス(ビークル)の引数を変更したくないので、サブクラス(Carなど)内でオーバーライドしたいだけです。
test_car = Car(10)
print(test_car.new_position)
>>> (0,0)
しかし、それは私にこのエラーを与え続け、私の知る限り、あなたが達成しようとしているかを理解としてnew_position
TypeError: __init__() missing 1 required positional argument: 'new_position'
あなたは –
@MosesKoledoyeは注意掲載コードの正確なコピーを実行していることを確認してください 'new_position'が設定され、position''と同じではありませんスーパークラスで。 – jonrsharpe
@jonrsharpe違いに気付かなかった。うん、そう。 –