Skip to content

Latest commit

 

History

History
32 lines (23 loc) · 781 Bytes

20231117182646.md

File metadata and controls

32 lines (23 loc) · 781 Bytes

Enums

Using an enum (enumeration) in Python helps group related, immutable named constants. 🚦💡🔢

For example, if you have these 5 (individual) constants:

STATUS_ACTIVE = 1
STATUS_INACTIVE = 2
STATUS_PENDING = 3
STATUS_CANCELLED = 4
STATUS_COMPLETED = 5

It's easy to turn them into a clear, organized enum:

from enum import Enum

class Status(Enum):
    ACTIVE = 1
    INACTIVE = 2
    PENDING = 3
    CANCELLED = 4
    COMPLETED = 5

And now in your code it's clearer that they are grouped, because you would access the constants like Status.ACTIVE, Status.INACTIVE, etc

This improves readability, code organization (update them in one place), and reduces errors (enums prevent the use of arbitrary, potentially incorrect values).

#enum #constants