The Enum Trick Every Python Developer Needs to Master!

Learn the powerful Enum trick that will make your code cleaner, safer, and more maintainable!

The Enum Trick Every Python Developer Needs to Master!
Photo by Brett Harrison on Unsplash

Take your Python skills to the next level with Enums!

The Enum Trick Every Python Developer Needs to Master!

If you’re a Python developer, chances are you’ve used enum.Enum to create readable and maintainable code.

But did you know there's a powerful trick that can take your enums to the next level? This trick not only improves readability but also adds extra functionality with minimal effort.

In this article, we’ll explore an advanced Enum pattern that makes your Python code more robust, maintainable, and error-resistant.
5 Powerful F-String Tricks Every Python Developer Should Know!
Learn five powerful f-string techniques to write cleaner, faster, and more readable Python code.

Why Use Enums in Python?

Before we dive into the trick, let’s briefly understand why Enums are useful.

Enums help you define a set of named constants that make your code more readable and less error-prone. Instead of using magic numbers or hardcoded strings, you can use Enums to represent values with meaningful names.

Without Enums (Bad Practice):

STATUS_PENDING = "pending" 
STATUS_APPROVED = "approved" 
STATUS_REJECTED = "rejected" 
 
def process(status): 
    if status == "approved": 
        print("Processing approved request")

The issue? Strings are prone to typos and lack structure.

With Enums (Better 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")

Now, our code is more structured, and we get type safety with Enums.

The Enum Trick: Adding Extra Functionality

While standard Enums are great, they are still just basic constants. What if we could add extra metadata or behavior to each Enum member?


The Trick: Using @property and Custom Methods

We can enhance our Enums by adding properties and methods to provide additional functionality. Here’s how:

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]

How This Helps

Now, instead of managing descriptions separately, we can access them directly:

print(Status.PENDING.description)  # Output: The request is pending approval. 
print(Status.APPROVED.description)  # Output: The request has been approved.

This makes your Enums self-contained and more informative.


Taking It Further: Mapping Extra Data

You can also use dataclass-like behavior in Enums by storing additional attributes.

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.

Why This Trick Is a Game-Changer

  • Keeps related data together — No need for external mappings.
  • Improves maintainability — Changes to statuses don’t require modifying multiple parts of the code.
  • Enhances readability — Developers can quickly see available statuses and their properties.

Final Thoughts

Python’s enum.Enum is more than just a collection of constants. By adding properties, methods, and extra attributes, you can make Enums powerful tools in your applications. Whether you’re building a web app, an API, or a CLI tool, this trick will help you write cleaner, more structured code.

Next time you use Enums, think beyond basic constants — unlock their full potential with this powerful trick!

What are your favorite Python Enum tricks? Let’s discuss in the comments!


Photo by Bithinraj Mb on Unsplash