Python's sad, unimaginative Enum

Steven D'Aprano steve+comp.lang.python at pearwood.info
Mon May 13 22:00:13 EDT 2013


On Mon, 13 May 2013 13:00:36 +0200, Jean-Michel Pichavant wrote:

> ----- Original Message -----
>> That's the title of this little beast
>> http://www.acooke.org/cute/Pythonssad0.html if anybody's interested.
>> 
>> --
>> If you're using GoogleCrap™ please read this
>> http://wiki.python.org/moin/GoogleGroupsPython.
>> 
>> Mark Lawrence
>> 
>> --
>> http://mail.python.org/mailman/listinfo/python-list
>> 
>> 
> python 2.5
> 
> class Enum:
>   class __metaclass__(type):
>     def __iter__(self):
>       for attr in sorted(dir(self)):
>         if not attr.startswith("__"):
>           yield getattr(self, attr)
> 
> class Colours(Enum):
>   RED = "red"
>   GREEN = "green"

py> class Experience(Enum):
...     NOVICE = 'novice'
...     GREEN = 'green'
...     EXPERIENCED = 'experienced'
...     MASTER = 'master'
...
py>
py> Colours.GREEN == Experience.GREEN
True


Oops.


It's very easy to make something which does a few things that enums 
should do, and call it an Enum. It's much harder to do a lot of things 
that enums should do.



-- 
Steven



More information about the Python-list mailing list