PEP 354: Enumerations in Python

Ben Finney bignose+hates-spam at benfinney.id.au
Mon Feb 27 20:22:55 EST 2006


Tim Chase <python.list at tim.thechases.com> writes:
> Just a couple thoughts:

Appreciated.

>> An enumeration is an exclusive set of symbolic names bound
>> to arbitrary unique values.
>
> Uniqueness imposes an odd constraint that you can't have synonyms in
> the set:
>
>  >>> shades = enum({white:100, grey:50, gray:50, black:0})

I think a dict is best used for this purpose. An enumeration is for
when you want all the values in the collection to be unique, but don't
care particularly what those values *are*.

>  >     >>> Grades = enum('A', 'B', 'C', 'D', 'F')
>
> This produces the counter-intuitive result of
>
> 	>>> Grades.A > Grades.B
> 	False
>
> Sure, one can bung with it and create the set of grades backwards,
> but then iteration becomes weird.  Perhaps the ability to override
> the comparitor?

Again, if you actually want specific values associated with each
member of the collection, I think a dict is best.

Note that the enumeration values *can* be dict keys, so you could
associate the values with other objects that way.

-- 
 \           "I was in a bar the other night, hopping from barstool to |
  `\     barstool, trying to get lucky, but there wasn't any gum under |
_o__)                                    any of them."  -- Emo Philips |
Ben Finney



More information about the Python-list mailing list