Buckets:
Objects and Classes
Enums
Enums (short for enumerations) are a powerful yet underappreciated feature in object-oriented design. They allow you to define a fixed set of named constants that improve clarity, type safety, and maintainability in your system. Used correctly, enums can make your code more expressive, self-documenting, and resilient to errors.
What is an Enum?
An enum is a special data type that defines a collection of constant values under a single name. Unlike primitive constants or string literals, enums are type-safe, which means you can’t assign just any value to a variable declared as an enum type. They ensure that a variable can only take one out of a predefined set of valid options.
[!info] In short: If a value can only be one of a predefined set of options, use an enum.
Why Use Enums?
Enums provide several key advantages over plain constants or strings:
- Avoid “magic values”: No more scattered strings or integers like
"PENDING"or3in your code. - Improve readability: Enums make your intent clear —
OrderStatus.SHIPPEDis far more descriptive than3. - Enable compiler checks: The compiler validates enum usage, catching typos and invalid assignments early.
- Support IDE features: Most IDEs provide auto-completion and refactoring tools for enum values.
- Reduce bugs: You can’t accidentally assign a random string or number that doesn’t belong to enum.
Example Enums:
Enums are perfect for defining categories or states that rarely change.
- Order States (e.g.,
PENDING,IN_PROGRESS,COMPLETED) - User Roles (e.g.,
ADMIN,CUSTOMER,DRIVER) - Vehicle Types (e.g.,
CAR,BIKE,TRUCK) - Directions (e.g.,
NORTH,SOUTH,EAST,WEST) By using enums instead of raw strings, you make your system easier to understand and harder to misuse.
Simple Enum
Let’s start with a simple example representing the status of an order in an e-commerce application.
from enum import Enum
# Simple Enum
class OrderStatus(Enum):
PLACED = "PLACED"
CONFIRMED = "CONFIRMED"
SHIPPED = "SHIPPED"
DELIVERED = "DELIVERED"
CANCELLED = "CANCELLED"
This enum defines a finite set of valid states an order can have. Nothing else is allowed.
Using it in code:
status = OrderStatus.SHIPPED
if status == OrderStatus.SHIPPED:
print("Your package is on the way!")
Enums with Properties and Methods
Enums can have additional data and even behavior. This makes them even more powerful.
Let’s consider a Coin enum that represents U.S. coins and their denominations.
from enum import Enum
class Coin(Enum):
PENNY = 1
NICKEL = 5
DIME = 10
QUARTER = 25
def __init__(self, value):
self.coin_value = value
def get_value(self):
return self.coin_value
total = Coin.DIME.get_value() + Coin.QUARTER.get_value() # 35
This is far more elegant and safe than using integers directly. Enums help you define a fixed set of well-known values, giving structure and clarity to your data. But what if you want to define a common set of behaviors that different classes can implement in their own way?
Xet Storage Details
- Size:
- 3.31 kB
- Xet hash:
- 583fbbc77aac10be1ce4ee5b0590d14b1942a05b3fcf753a8c4c26585e479ccb
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.