Created
May 31, 2025 01:40
-
-
Save mschulz/4084660c1b097ac5a0a547a097370ca7 to your computer and use it in GitHub Desktop.
Uses of Enum
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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