PEP 354: Enumerations in Python

Dan Sommers me at privacy.net
Mon Feb 27 10:57:43 EST 2006


On Mon, 27 Feb 2006 03:13:08 -0600,
Tim Chase <python.list at tim.thechases.com> wrote:

> Same could go for days of the week:

>>>> dow=enum(("sunday", 0), ("monday", 1), 
> ("start_of_work_week", 1), ... ("friday", 5), 
> ("end_of_work_week", 5)...)

Not really:  In some parts of the world, calendar weeks begin on Monday
and end on Sunday, and in other parts of the world, work weeks begin on
Sunday and end on Thursday.  ISTM that if you want to do anything with
the value of a given enumeration tag (including make greater/less than
comparisons, sort, etc.), then a dictionary might be better than an
enum.  Also, enumeration tags like "end_of_work_week" should be assigned
at run-time based on locale and/or preferences settings (not that you
couldn't have stuck the code above into a preferences file...).

> Given more little grey cells (or gray cells?), I'm sure I could come
> up with more examples of when it's handy to have duplicate values in
> an enum...

I don't know:  If you're using enums as names for otherwise meaningful
values, then you're right.  But if you're using enums as meaningful
names for pairwise distinct concepts, then I think otherwise.  IMO, if
your program breaks if someone re-assigns the values randomly, then a
better solution than an enum exists.  (Or maybe that's because I think
of the tags as nothing more than keywords that work like distinct
values.)

Regards,
Dan

-- 
Dan Sommers
<http://www.tombstonezero.net/dan/>
"I wish people would die in alphabetical order." -- My wife, the genealogist



More information about the Python-list mailing list