[Python-Dev] PEP 435: initial values must be specified? Yes

Ethan Furman ethan at stoneleaf.us
Sun May 5 22:09:50 CEST 2013


On 05/05/2013 10:07 AM, � wrote:> I'm chiming in late, but am I the only one who's really bothered by the syntax?
>
> class Color(Enum):
>      red = 1
>      green = 2
>      blue = 3

No, you are not only one that's bothered by it.  I tried it without assignments until I discovered that bugs are way too 
easy to introduce.  The problem is a successful name lookup looks just like a name failure, but of course no error is 
raised and no new enum item is created:

--> class Color(Enum):
...     red, green, blue
...

--> class MoreColor(Color):
...     red, orange, yellow
...

--> type(MoreColor.red) is MoreColor
False

--> MoreColor.orange
<MoreColor.orange: 4>   # value should be 5

About the closest you going to be able to get is something like:

def e(_next=[1]):
     e, _next[0] = _next[0], _next[0] + 1
     return e

class Color(Enum):
     red = e()
     green = e()
     blue = e()

and you can keep using `e()` for all your enumerations, since you don't care what actual value each enumeration member 
happens to get.

--
~Ethan~


More information about the Python-Dev mailing list