Is enum iteration order guaranteed?

Ethan Furman ethan at stoneleaf.us
Tue Jan 10 03:35:25 EST 2017


On 01/09/2017 10:22 PM, Steven D'Aprano wrote:
> On Tuesday 10 January 2017 16:55, Ethan Furman wrote:
>> On 01/09/2017 09:18 PM, Steven D'Aprano wrote:
>>
>>> The docs say that enums can be iterated over, but it isn't clear to me
>>> whether they are iterated over in definition order or value order.
>>>
>>> If I have:
>>>
>>> class MarxBros(Enum):
>>>       GROUCHO = 999
>>>       CHICO = 5
>>>       HARPO = 11
>>>       ZEPPO = auto()
>>>       GUMMO = -1
>>>
>>> GROUCHO, CHICO, HARPO, ZEPPO, GUMMO = list(MarxBros)
>>
>> In Python 3 it is always definition order.
>
> I only care about Python 3 for this.
>
> Did I miss something in the docs, or should this be added?

In https://docs.python.org/3/library/enum.html#creating-an-enum, fifth paragraph down:

   Enumerations support iteration, in definition order ...

--
~Ethan~



More information about the Python-list mailing list