The Shape class contains a factory function that returns objects of different classes.
#!/usr/bin/env python
class Color(object):
@staticmethod
def factory(type):
if type == "Red": return Red()
if type == "Blue": return Blue()
assert 0, "Bad color creation: " + type
class Red(Color):
def tell(self): print( "Red ")
class Blue(Color):
def tell(self): print( "Blue ")
def colorNameGen():
""" Generate color name strings: """
cols = Color.__subclasses__()
for type in cols:
yield type.__name__
colors = []
for i in colorNameGen():
colors.append( Color.factory(i))
for col in colors:
col.tell()