![](https://secure.gravatar.com/avatar/f3ba3ecffd20251d73749afbfa636786.jpg?s=120&d=mm&r=g)
On Thu, Jul 28, 2011 at 10:56 AM, Barry Warsaw <barry@python.org> wrote:
Again, looking at how I've used them extensively over the last several years, I would much rather write
class Colors(Enum): red = 1 green = 2 blue = 3
than
red = NamedValue('red', 1) green = NamedValue('green', 2) blue = NamedValue('blue', 3)
To me, the duplication is jarring and error prone.
Yeah, I'd actually be inclined to define such values programmatically rather than writing them out manually like that: _named_colours = dict( red=0xFF0000, green=0x00FF00, blue=0x0000FF, ) globals().update((k, namedvalue(k, v)) for k, v in _named_colours) (where namedvalue is the value based factory function I mentioned in the recipe post) However, my contention is that the fundamentally interesting operation is associating names with values (as your EnumValue class does). Enums and their ilk are then just syntactic sugar for defining groups of such values without needing to repeat yourself. Cheers, Nick. -- Nick Coghlan | ncoghlan@gmail.com | Brisbane, Australia