Skip to content

Instantly share code, notes, and snippets.

@mschulz
Created May 31, 2025 01:40
Show Gist options
  • Save mschulz/4084660c1b097ac5a0a547a097370ca7 to your computer and use it in GitHub Desktop.
Save mschulz/4084660c1b097ac5a0a547a097370ca7 to your computer and use it in GitHub Desktop.
Uses of Enum
# Basic Approach
from enum import Enum
class Status(Enum):
PENDING = "pending"
APPROVED = "approved"
REJECTED = "rejected"
def process(status: Status):
if status == Status.APPROVED:
print("Processing approved request")
# Extended by use of @property
from enum import Enum
class Status(Enum):
PENDING = "pending"
APPROVED = "approved"
REJECTED = "rejected"
@property
def description(self):
descriptions = {
"pending": "The request is pending approval.",
"approved": "The request has been approved.",
"rejected": "The request was rejected."
}
return descriptions[self.value]
# Over the top approach
from enum import Enum
class Status(Enum):
PENDING = ("pending", "🟡")
APPROVED = ("approved", "✅")
REJECTED = ("rejected", "❌")
def __init__(self, value, emoji):
self._value_ = value
self.emoji = emoji
@property
def description(self):
descriptions = {
"pending": "The request is pending approval.",
"approved": "The request has been approved.",
"rejected": "The request was rejected."
}
return descriptions[self.value]
# Usage
print(Status.PENDING.emoji) # Output: 🟡
print(Status.APPROVED.description) # Output: The request has been approved.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment