Paul's Python CoursePPC

Enums

Not met yet

An Enum is a fixed set of named choices: class Stamp(Enum): with FIRST = 135 inside. Each member has a .name and a .value, but a member is not its value: compare members with members, stamp == Stamp.FIRST. Stamp(85) finds the member with that value, and looping over the class gives every member.

Try it

main.py
from enum import Enum

class Stamp(Enum):
    FIRST = 135
    SECOND = 85

print(Stamp.FIRST, Stamp.FIRST.value, Stamp(85))

Output

Press Run and what your program prints shows up here.

Easy to get wrong

  • Thinks an Enum member is the same thing as its value, so Stamp.FIRST is 135.
  • Thinks an Enum member compares equal to its value, or to its name written as text.
  • Thinks calling an Enum with a value, like Stamp(85), makes a new member rather than finding the existing one.

Where it comes up