Codectionary / Developer documentation / Python

Named States with Enum

Enum gives a fixed set of symbolic members distinct identities and associated values. Value lookup and name lookup are different operations. Run as a Python 3.11+ script.

Syntax

class State(Enum): READY = "ready"; State("ready"); State["READY"]

Examples

Named States with Enum

Run as a Python 3.11+ script.

from enum import Enum
class State(Enum):
    READY = "ready"
    DONE = "done"
state = State("ready")
print(state.name)
print(state.value)
print(state is State.READY)
  1. Expected output: READY ready True
  2. Calling State converts a known stored value into its canonical enum member. Unknown values raise ValueError instead of silently creating a state.

Best practices

  • Serialize a deliberate stable value rather than the debugging representation.
  • Use explicit values when enum members are part of an external format.

Runtime & versions

Python interpreter. Feature availability depends on the language and runtime version used by your project; consult the official reference below.

Python language reference

Specifications & further reading