Using type :
class Fruit(object): def __init__(self, color, shape): self.color = color self.shape = shape apple = {'color': 'red', 'shape': 'sphere'} pear = {'color': 'yellow', 'shape': 'cone'} melon = {'color': 'green', 'shape': 'prolate'} g = globals() for clsname, attrs in [('Apple', apple), ('Pear', pear), ('Melon', melon)]: def temp(attrs): g[clsname] = type(clsname, (Fruit,), { '__init__': lambda self: Fruit.__init__(self, **attrs) }) temp(attrs)
>>> a = Apple() >>> p = Pear() >>> m = Melon() >>> assert a.color == 'red' and a.shape == 'sphere' >>> assert p.color == 'yellow' and p.shape == 'cone' >>> assert m.color == 'green' and m.shape == 'prolate'
source share