I have a base class from which I would like to make many subclasses. The subclasses differ only in the arguments used to call the base class during instantiation. The example below shows how to make a subclass Apple
. Is there a way to do this programatically, without having to code the subclass' __init__
method? This seems like a job for metaclasses, but in this case I cannot modify the base class.
apple = {'color': 'red', 'shape': 'sphere'}
pear = {'color': 'yellow', 'shape': 'cone'}
melon = {'color': 'green', 'shape': 'prolate'}
class Fruit(object):
def __init__(self, color, shape):
self.color = color
self.shape = shape
class Apple(Fruit):
def __init__(self):
Fruit.__init__(self, **apple)