Skip to content

enum

Note

Enumerations are 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.

Defining an Enumeration

Each member of an enum needs an assigned value. Values can be different types.

from enum import Enum

class Color(Enum):
    RED = 1
    GREEN = 2
    BLUE = 3
    BLACK = 4
    WHITE = 5

enum.auto() Function

This generates unique values for each member of the enumeration.

from enum import Enum, auto

class Color(Enum):
    RED = auto()
    GREEN = auto()
    BLUE = auto()
    BLACK = auto()
    WHITE = auto()

Accessing Members

Color.RED
Color.RED.name
Color.RED.value
Color["RED"]
Color.RED
'RED'
1
Color.RED: 1

Iteration

for color in Color:
    print(color)
Color.RED
Color.GREEN
Color.BLUE
...

Comparing Members

Color.RED is Color.RED
Color.RED is Color.GREEN
True
False

Flags

Flag-Enums are used to define a set of flags for another member.

For example, a user may have READ, WRITE, EXECUTE, and DELETE access rights. These can be combined into a single ADMIN flag.

from enum import Flag, auto

class AccessRights(Falg):
    READ = auto()
    WRITE = auto()
    EXECUTE = auto()
    DELETE = auto()
    ADMIN = READ | WRITE | EXECUTE | DELETE