[Python-Dev] PEP 435 -- Adding an Enum type to the Python standard library

Nick Coghlan ncoghlan at gmail.com
Mon Apr 22 02:42:04 CEST 2013


On Mon, Apr 22, 2013 at 10:28 AM, Barry Warsaw <barry at python.org> wrote:
> On Apr 22, 2013, at 09:02 AM, Nick Coghlan wrote:
>
>>Iteration order matters a lot if you don't want people complaining about
>>enums being broken:
>>
>>  class Days(enum.Enum):
>>    Monday = 1
>>    Tuesday = 2
>>    Wednesday = 3
>>    Thursday = 4
>>    Friday = 5
>>    Saturday = 6
>>    Sunday = 7
>
> Sorry, that's still not a complete use case.  I don't see why you'd depend on
> iteration order over Days for any particular functionality.

You mean other than printing the days of the week in order without
needing to worry about the specific values assigned to them?

Using sort-by-name also introduces other weirdness, such as subclasses
potentially inserting their values in the middle of inherited names,
rather than appending to the end as one might reasonably expect.

While using sort-by-name is better than not providing a consistent
ordering at all, using definition order is substantially less
surprising than sorting by key name, and PEP 3115 and
collections.OrderedDict makes that easy to support in Python 3.x.

The fact that this will make for a behavioural difference between the
standard library and flufl.enum does *not* count as an argument for
making the behaviour of the standard library version less intuitive
(if that was a valid argument, the 3.3+ ipaddress module would look a
*lot* more like it's ipaddr inspiration).

> Besides, if you
> did, I think it would be better to derive Days from IntEnum and then iteration
> order is guaranteed over the values.

But only at the cost of breaking the other guarantees provided by a
standard enumeration.

Cheers,
Nick.

--
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia


More information about the Python-Dev mailing list