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 Jakob Owens 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 and you’re not yet utilizing the full power of Enum, you’re leaving some serious readability, safety, and elegance on the table.

Today, I’m going to show you one surprisingly powerful trick with Python’s Enum that can level up your code, especially when you're dealing with constants, config settings, or state machines. And no, this isn't just about replacing a bunch of strings with enums — it's better.

Let’s dive in.

First, Why Use Enums At All?

Before the trick, let’s quickly align on why Enums exist in Python in the first place.

Enums (short for enumerations) give you:

  • Self-documenting code: Status.ACTIVE is clearer than "active"
  • Avoided typos: IDEs will catch Status.ACITVE (whoops) but not "acitve"
  • Safer comparisons: They prevent you from comparing apples to oranges — or strings to integers.

Enums are especially handy when you have a known set of states, types, or categories: user roles, order statuses, environment names, etc.

But here’s the thing — Enums can do more than hold values.

The Trick: Custom Behavior in Your Enum

Here’s the trick: you can add methods and properties to Enums, just like any other class.

Let’s say you’re working with a UserRole enum:

from enum import Enum 
 
class UserRole(Enum): 
    ADMIN = "admin" 
    EDITOR = "editor" 
    VIEWER = "viewer"

Looks fine, right? But what if each role needs different permissions? You might be tempted to define a separate dictionary elsewhere:

permissions = { 
    "admin": ["create", "edit", "delete"], 
    "editor": ["edit"], 
    "viewer": [] 
}

But this scatters logic across your codebase. The better way? Put the behavior right into the enum:

from enum import Enum 
 
class UserRole(Enum): 
    ADMIN = "admin" 
    EDITOR = "editor" 
    VIEWER = "viewer" 
 
    @property 
    def permissions(self): 
        if self == UserRole.ADMIN: 
            return ["create", "edit", "delete"] 
        elif self == UserRole.EDITOR: 
            return ["edit"] 
        return []

Now, using it is beautiful and intuitive:

print(UserRole.ADMIN.permissions)  # ['create', 'edit', 'delete']

No more scattered dictionaries. No more string lookups. Just clean, object-oriented code.

Clean It Up with @classmethod or dict Lookup

If the if/elif chain is rubbing you the wrong way (I feel you), clean it up using a dictionary:

class UserRole(Enum): 
    ADMIN = "admin" 
    EDITOR = "editor" 
    VIEWER = "viewer" 
 
    _permissions_map = { 
        "admin": ["create", "edit", "delete"], 
        "editor": ["edit"], 
        "viewer": [] 
    } 
 
    @property 
    def permissions(self): 
        return self._permissions_map[self.value]

Even better: if you want a method instead of a property:

def can(self, action): 
    return action in self.permissions

Now you can say:

if role.can("edit"): 
    ...

That’s readable. That’s elegant. That’s Python.

When Should You Use This Trick?

Use this Enum pattern when:

  • You’re tempted to write a separate lookup table based on enum values.
  • Each enum member needs different behavior or configuration.
  • You want your code to read like English.

It shines in:

  • Feature flags or access control
  • Workflow states
  • Environment configs (e.g., dev vs prod behavior)
  • Mapping values to UI labels or colors

Bonus Tip: Auto Values with auto()

You can even automate enum values if you don’t care about what they are:

from enum import Enum, auto 
 
class Status(Enum): 
    TODO = auto() 
    IN_PROGRESS = auto() 
    DONE = auto()

This is perfect when the value doesn’t matter — only the identity does.


Final Thoughts

Enums in Python aren’t just dumb lists of constants — they can be powerful, expressive containers of logic.

By embedding behavior and related data directly into your Enum, you write more readable, maintainable, and Pythonic code.

So the next time you’re reaching for a string-based lookup or a conditional soup, remember: your Enum can do that. And do it better.


What about you?

Have you used Enums like this in your own projects? Got a clever pattern or a nightmare Enum story to share?

Let me know in the comments 👇 — I’d love to hear your take!


Follow for more Python tips, tricks, and deep dives that’ll actually make your code better.


Photo by Amr Taha™ on Unsplash