
I'm evil. I just had a very bad thought, and I think I kinda like it. Python 3.3.0 (v3.3.0:bd8afb90ebf2, Sep 29 2012, 10:57:17) [MSC v.1600 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information.
from enum import Enum, _
class Color(Enum): ... RED ... GREEN = _(None, 'green') ... BLUE = _(None, 'blue', hex='0000FF') ... def __init__(self): ... print('__init__', repr(self), self.args, self.kwargs) ... def dump(self): ... print(self, self.args, self.kwargs) ... __init__ <EnumValue 'Color.RED': 0> () {} __init__ <EnumValue 'Color.GREEN': 1> ('green',) {} __init__ <EnumValue 'Color.BLUE': 2> ('blue',) {'hex': '0000FF'}
print(repr(Color)) <enum __main__.Color {<EnumValue 'RED': 0>, <EnumValue 'GREEN': 1>, <EnumValue 'BLUE': 2>}>
for e in Color: ... e.dump() ... Color(e).dump() ... Color.RED () {} Color.RED () {} Color.GREEN ('green',) {} Color.GREEN ('green',) {} Color.BLUE ('blue',) {'hex': '0000FF'} Color.BLUE ('blue',) {'hex': '0000FF'}
When you request an attribute on the EnumValue that doesn't exist, it gets it from the owning Enum class. If the attribute is an instance method (or static method - they have the same type) then it gets turned into an instance method of the EnumValue. Also, when the EnumValue becomes owned by Enum, Enum.__init__ is called as an EnumValue instance method (with no parameters). This actually makes a kind of perverse sense. Conceptually the actual EnumValues are the instances of the Enum (and in fact, I've made them pretend to actually be so). I think it also entirely does away with the need for subclasses of EnumValue and EnumMeta - you can do everything by specifying attributes and behaviour in the Enum instance methods. I've pushed this to https://bitbucket.org/magao/enum - have a play and see how evil you think this is. Tim Delaney