Types

Enums

Enum defines symbolic names for a fixed set of values.

Enum defines a fixed set of named values. This makes states and modes easier to read than raw strings scattered through a program.

Each enum member has a name and a value. Comparing enum members is explicit and helps avoid typos that plain strings would allow.

Use enums when a value must be one of a small known set: statuses, modes, directions, roles, and similar choices.

An enum member has a symbolic name and an underlying value. The symbolic name is what readers usually care about in code.

Source

from enum import Enum

class Status(Enum):
    PENDING = "pending"
    DONE = "done"

current = Status.PENDING
print(current.name)
print(current.value)

Output

PENDING
pending
An enum names a fixed set of symbolic values; no new members appear at runtime.

Compare enum members with enum members, not with raw strings. This keeps the set of valid states explicit.

Source

print(current is Status.PENDING)
print(current == "pending")

Output

True
False

Notes

See also

Run the complete example

Example code

Expected output

PENDING
pending
True
False

Execution time appears here after you run the example.