Working with enum in python

Working with enum in python

An enumeration is a set of symbolic names (members) bound to unique, constant values. Within an enumeration, the members can be compared by identity, and the enumeration itself can be iterated over.

The enum module is used for creating enumerations in Python. Enumerations are created with the class keyword or with the functional API.

There are specific derived enumerations

  • enum.IntEnum
  • enum.IntFlag
  • enum.Flag

Python "enum" Example

Lets create simple enum for colors

from enum import Enum

class Color(Enum):
    RED = 1
    GREEN = 2
    BLUE = 3

print(Color.RED)

# iterate and print all colors
for color in Color:
    print(color)

Ref: https://docs.python.org/dev/library/enum.html