33. Enumerations

If you are working with enumerations, use the enum package. In the example below, we have students who may be part, half or full time. If we simply declared these states with normal variables, they may be overwritten and there will be no context. On the other hand, if we use IntEnum, once declared, these states are immutable and provide context.

33.1. Don’t do this

1PART_TIME = 1
2HALF_TIME = 2
3FULL_TIME = 3

33.2. Do this

1from enum import IntEnum
2
3class StudentType(IntEnum):
4    PART_TIME = 1
5    HALF_TIME = 2
6    FULL_TIME = 3