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

Ethan Furman ethan at stoneleaf.us
Fri Apr 26 00:36:53 CEST 2013


On 04/25/2013 03:23 PM, Barry Warsaw wrote:
> On Apr 22, 2013, at 10:42 AM, Nick Coghlan wrote:
>
>> 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?
>
> My point is, "days of the week" has a natural ordering, so why wouldn't you
> use IntEnum for that?  Problem solved.

Because by using an IntEnum we lose the Enum type protection, which is one of the reasons to want an Enum type to begin 
with.


> There's no natural ordering for things like colors or animals, so the values
> don't matter.  I claim that neither does the repr or iteration order except
> that the former should be *predictable* and it would be nice to define the
> latter, but that's not actually necessary.  Undefined iteration order would be
> just as fine for Enum.

People like things sorted (or am I alone here?)  There are three obvious natural orderings:

   1 - value

   2 - definition order

   3 - name

And that's my order of preference for 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.
>
> I don't see how iteration order could affect how you'd write the derived class
> syntax.

It probably wouldn't, but if I had:

--> class Color(Enum):
...     red = 1
...     blue = 2
...     green = 3

--> class MoreColor(Color):
...     cyan = 4
...     magenta = 5
...     yellow = 6

I would be very unhappy with:

--> list(MoreColor)
[
   MoreColor.blue,
   MoreColor.cyan,
   MoreColor.green,
   MoreColor.magenta,
   MoreColor.red,
   MoreColor.yellow,
]

because 1) it's not the order I defined it in; and 2) it's not in value order.

--
~Ethan~


More information about the Python-Dev mailing list