Home > front end >  Is iteration order over an enum deterministic?
Is iteration order over an enum deterministic?

Time:03-25

I am using the aenum library.

from aenum import Enum as Aenum

class Availability(Aenum):
    _init_ = "value string"
    PART_TIME = 1, "Part-Time"
    FULL_TIME = 2, "Full-Time"
    BOTH = 3, "Both"

    def __str__(self):
        return self.string

for value, string in Availability:
   print(value)
   print(string)

Empirically it seems to go in order of definition but I was curious if this was guaranteed.

CodePudding user response:

From the documentation on Github:

Enumerations support iteration. In Python 3.x definition order is used; in Python 2.x the definition order is not available, but class attribute _order_ is supported; otherwise, value order is used if posible, otherwise alphabetical name order is used.

CodePudding user response:

aenum.Enum is described as

a Python stdlib Enum-compatible data type

and the stdlib enum.Enum is documented as

Enumerations support iteration, in definition order

  • Related