Is enum iteration order guaranteed?
Steven D'Aprano
steve+comp.lang.python at pearwood.info
Tue Jan 10 01:22:34 EST 2017
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?
[...]
>> On that related note, how would people feel about a method that injects
>> enums into the given namespace?
[...]
> Or you can do globals().update(MarxBros.__members__).
Sweet!
--
Steven
"Ever since I learned about confirmation bias, I've been seeing
it everywhere." - Jon Ronson
More information about the Python-list
mailing list