Technical skill practice

Enum in Python: Names, Values and Explicit Conversion

Python Enum gives named members to a defined set of values. A member’s name and value are different: State.READY can have the name READY and a stored value such as ready. Convert by value with State(value), or look up a name with State[name].

Start with a small, explicit task

Represent a fictional review workflow with queued, ready and sent states. Convert an incoming value into a member, inspect the display name and deliberately reject an unknown value. The example defines labels only; it does not enforce an application’s allowed transitions or persistence rules.

Run the complete example

This original example was checked with Python 3.12.14. Save it as python_enum.py and run it with Python 3.12. It uses only the standard library. The assertions check the stated results.

Expected output

READY ready

['queued', 'ready', 'sent']

unknown value rejected

from enum import Enum
class State(Enum):
    QUEUED = "queued"
    READY = "ready"
    SENT = "sent"
incoming = State("ready")
assert incoming is State.READY
assert State["READY"] is State.READY
assert incoming.name == "READY" and incoming.value == "ready"
try:
    State("paused")
except ValueError:
    rejected = "unknown value rejected"
else:
    raise AssertionError("expected invalid value")
print(incoming.name, incoming.value)
print([member.value for member in State])
print(rejected)

Explain the result

The two lookup styles arrive at the same READY member through different inputs. The call uses its stored lowercase value, while brackets use the uppercase member name. Iteration yields the three defined states in the order shown. The unknown incoming value produces ValueError, giving the caller a concrete boundary to validate.

Nothing in this definition says that SENT can return to QUEUED or that READY is the initial state. Those transition rules require separate application logic. Likewise, a database or JSON serializer may need an explicit decision to store .value or .name; the presence of an Enum does not guarantee that another system understands it automatically.

Avoid these mistakes

Do not interchange names and values in external data. If you change a serialized value later, consider existing consumers instead of assuming that renaming a label is harmless. Keep plain Enum distinct from specialized classes such as IntEnum when reasoning about comparisons with ordinary values.

Questions, answered

How do I look up a member by its value?

Call the Enum class with that value, as State("ready") does here.

Does Enum enforce workflow transitions?

No. This definition enumerates states; transition validation is a separate task.

Sources and examples

ResumizeAI team. Examples are fictional teaching material, not customer results. Replace sample details with your own facts.

Your experience. A complete resume.

Build in Resumize with ATS-friendly templates, guided editing and a live preview. Start free, then upgrade when you need more.

Free account required. Free-plan PDF exports include a watermark.