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

Barry Warsaw barry at python.org
Sat Apr 20 20:10:32 CEST 2013

On Apr 13, 2013, at 08:37 AM, Tim Delaney wrote:

>Just using definition order as the stable iteration order would do the
>trick - no need for any comparisons at all. Subclasses (e.g. IntEnum) can
>then override it.

I think this isn't possible if we want to keep backward compatibility with
earlier Pythons, which I want to do.  OTOH, we have another natural sorting
order for base Enums sitting right in front of us: the attribute name.  These
have to be unique and ordered, so why not use this for both the __repr__() and
the base Enum __iter__()?  IntEnum can override __iter__() to iterate over
item values, which also must be ordered.

I just made this change to flufl.enum and it seems to work well.

>>> from flufl.enum import Enum
>>> A = Enum('A', 'a b c')
>>> A
<A {a: 1, b: 2, c: 3}>
>>> for item in A: print(item)
>>> B = Enum('B', 'c b a')
>>> B
<B {a: 3, b: 2, c: 1}>
>>> for item in B: print(item)
>>> from flufl.enum import IntEnum
>>> C = IntEnum('C', 'c b a')
>>> C
<C {a: 3, b: 2, c: 1}>
>>> for item in C: print(item)


More information about the Python-Dev mailing list