2. Enumerations
If you are working with enumerations, use the enum module. In the example below, students may be part-time, half-time, or full-time. If you declare these states as plain variables, they can be overwritten and provide little context. IntEnum makes the intent explicit and keeps the values grouped under a meaningful type.
Enums give invalid states fewer places to hide and make call sites easier to read than raw integers or strings. They also centralize the valid choices, which helps both tooling and future maintenance.
2.1. Don’t do this
1PART_TIME = 1
2HALF_TIME = 2
3FULL_TIME = 3
2.2. Do this
1from enum import IntEnum
2
3class StudentType(IntEnum):
4 PART_TIME = 1
5 HALF_TIME = 2
6 FULL_TIME = 3