Mastery
Mastery/Python/J. Stdlib gems
T1 · high-leverage

enum: auto(), Flag (bitwise combos), StrEnum/IntEnum

auto() assigns values so you don't have to (and don't accidentally duplicate one). Flag members combine with | like real bitflags, and support in for membership checks -- built for "which of these options are set," not "which one option is this." StrEnum (3.11+) and IntEnum members ARE actual str/int instances -- they compare equal to plain strings/ints directly, unlike a plain Enum.

python
from enum import Enum, Flag, auto, StrEnum, IntEnum

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

print("auto() assigned values:", list(Color))

class Permission(Flag):
    READ = auto()
    WRITE = auto()
    EXECUTE = auto()

rw = Permission.READ | Permission.WRITE
print("combined flags:", rw)
print("READ in rw:", Permission.READ in rw)
print("EXECUTE in rw:", Permission.EXECUTE in rw)
python
class Status(StrEnum):
    ACTIVE = "active"
    INACTIVE = "inactive"

print("StrEnum member IS a str:", isinstance(Status.ACTIVE, str))
print("compares equal to a plain string directly:", Status.ACTIVE == "active")

class Priority(IntEnum):
    LOW = 1
    HIGH = 10

print("IntEnum supports int comparisons directly:", Priority.HIGH > Priority.LOW, Priority.HIGH > 5)

Interview angle

A quick "how current is your Python" check similar to match/case — StrEnum is 3.11+, so knowing it exists (and that its members compare equal to plain strings, unlike a regular Enum) signals someone's kept up with recent releases. Flag's bitwise-combination behavior is also a nice practical question: "how would you represent a set of independently-togglable options?" — testing whether someone reaches for Flag or falls back to a set of booleans or raw integer bit-twiddling.

In the industry

Plain Enum is the default choice for a closed set of named constants (statuses, categories, modes) instead of magic strings or integers scattered through a codebase — it gives you type safety and an authoritative list of valid values in one place. StrEnum specifically solves a real, common pain point: needing enum-like safety internally while still serializing cleanly to plain strings for JSON APIs or database columns, without the awkward .value access a regular Enum requires everywhere it crosses that boundary.