= Enumeration Programming = === Why, When === This Implementation is really near to the UML description of <>. It uses new style class. === Code === {{{ #!python # code is public domain class Enumeration(object): def __new__(cls, arg): if hasattr(cls, arg): return getattr(cls,arg) else: return object.__new__(cls, arg) def __init__(self, name): self._name = name setattr(self.__class__, name, self) def __str__(self): return '#%s' % self._name def __repr__(self): return "%s('%s')" % (self.__class__.__name__, self._name) def getEnumerationSet(cls): result = set() for attr in dir(cls): attr = getattr(cls, attr) if isinstance(attr, Enumeration): result.add(attr) return result getEnumerationSet = classmethod(getEnumerationSet) }}} === Example === {{{ #!python class PrimaryColorKind(Enumeration): pass PrimaryColorKind('Rouge') PrimaryColorKind('Vert') PrimaryColorKind('Bleu') print PrimaryColorKind.Rouge, PrimaryColorKind.Vert, PrimaryColorKind.Bleu print PrimaryColorKind.getEnumerationSet() class ColorKind(PrimaryColorKind): pass ColorKind('Violet') print ColorKind.Rouge, ColorKind.Violet print ColorKind.getEnumerationSet() print repr(ColorKind.Rouge), repr(ColorKind.Violet) assert(ColorKind.Rouge is ColorKind('Rouge')) }}} output is : {{{ #Rouge #Vert #Bleu Set([PrimaryColorKind('Vert'), PrimaryColorKind('Rouge'), PrimaryColorKind('Bleu')]) #Rouge #Violet Set([PrimaryColorKind('Vert'), PrimaryColorKind('Rouge'), ColorKind('Violet'), PrimaryColorKind('Bleu')]) PrimaryColorKind('Rouge') ColorKind('Violet') mirville Python 79 % python Enumeration.py #Rouge #Vert #Bleu Set([PrimaryColorKind('Vert'), PrimaryColorKind('Rouge'), PrimaryColorKind('Bleu')]) #Rouge #Violet Set([PrimaryColorKind('Vert'), PrimaryColorKind('Rouge'), ColorKind('Violet'), PrimaryColorKind('Bleu')]) PrimaryColorKind('Rouge') ColorKind('Violet') }}}