This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# https://stackoverflow.com/a/5961102 | |
# Note python3 has a different declaration than the so post | |
class ShapeFactory(type): | |
"""Shape Factory""" | |
def __call__(cls, selector, *args): | |
if cls is Shape: | |
# select a new object | |
print('factory build:', selector) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# https://stackoverflow.com/a/5961102 | |
class Shape(object): | |
def __new__(cls, *args): | |
selector = args[0] | |
print('factory build:', selector) | |
if cls is Shape: | |
# Create a subclassed shape from a name | |
if selector == 'rectangle': return super(Shape, cls).__new__(Rectangle) | |
if selector == 'triangle': return super(Shape, cls).__new__(Triangle) |