Is enum iteration order guaranteed?

Steven D'Aprano steve+comp.lang.python at pearwood.info
Tue Jan 10 00:18:09 EST 2017


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)


is that guaranteed to do the right thing?

i.e. GROUCHO is MarxBros.GROUCHO, and similarly for the other members.




On that related note, how would people feel about a method that injects enums 
into the given namespace?

MarxBros._inject_(globals())

although maybe 

from MarxBros import *

would be more familiar syntax :-)





-- 
Steven
"Ever since I learned about confirmation bias, I've been seeing 
it everywhere." - Jon Ronson



More information about the Python-list mailing list