Let's say I'm trying to create a package for the users to make new classes of cars. Originally I have a simple code structure:
class Car:
def __init__(self, diesel_engine):
self._engine = diesel_engine
def _start(self):
pass
# Then the user can do
class Truck(Car):
pass
But now I want to enhance the functionality and cater to both conventional car and electric car in the package. The problem is that the constructor of the base class Car
would now need to accept either diesel_engine
or electric_motor
; and also, the class method _start
would perform different things accordingly (so for example, it will call _ignite
or _close_circuit
).
Am I better off adding the derived classes in my package:
class Conventional(Car):
def _start(self):
pass
class ElectricVehicle(Car):
def _start(self):
pass
or is there a better way by using, e.g., decorator? Essentially I want the end-user to make new derived classes by passing in appropriate arguments and deriving from a single base class (because all other class methods are identical except for _start
)