Wow - this ended up being more difficult that I'd anticipated. Ensuring that decorators work in Michael Foord-inspired enums, etc mandated a fairly big redesign. As it was you couldn't do something like:
class A():
a = 1
class B(Enum):
B = A.a
because when the name 'A' was resolved in the definition of class B, it was returning an int (the next enum value).
The end result may well still have some holes, but it's looking pretty good to me.
Enums are constructed like:
class Color(Enum):
RED, GREEN, BLUE
CYAN = 10
MAGENTA
YELLOW
BLACK
where the assigned enum starts a new count i.e. the above is 0, 1, 2, 10, 11, 12, 13. Arbitrary attributes may be assigned and not contribute to the enumeration so long as the object being assigned does not implement __index__ (if it does, it creates a discontiguous enumeration).
#!/usr/bin/env python3
import builtins
import collections
import operator
class EnumValue(int):
def __new__(cls, key, value):
e = super().__new__(cls, value)
super().__setattr__(e, 'key', key)
super().__setattr__(e, 'owner', None)
return e
def __setattr__(self, key, value):
raise TypeError("Cannot set attribute of type %r" % (type(self),))