I have this problem, for example I define a Vehicle class as such.
class Vehicle():
speed = 0
def __init__(self):
pass
def get_speed(self):
return Vehicle.speed
I have a class attribute speed which I will override in the Bike child class
class Bike(Vehicle):
speed = 20
if I instantiate a new object of type Bike I get this
>>> a = Bike()
>>> a.get_speed()
0
How do I make it to get without having to override parent class methods.
>>> a.get_speed()
20
I do have a solution but I don't know if it is a good programming practice or not, because instance variables should be unique to that instance, so now I am very much confused.
class Vehicle():
def __init__(self):
self.speed = 0
def get_speed(self):
return self.speed
class Bike(Vehicle):
def __init__(self):
self.speed = 20
should I re-declare it as an instance variable so that if I override self.speed = 20 in the constructor then I get
>>> a = Bike()
>>> a.get_speed()
20
Any help would be much appreciated, thanks a lot in advance.