DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Python Async/Sync: Advanced Blocking Detection and Best Practices (Part 2)
  • Pydantic: Simplifying Data Validation in Python
  • Bridging Graphviz and Cytoscape.js for Interactive Graphs
  • Getting Started With Snowflake Snowpark ML: A Step-by-Step Guide

Trending

  • Combining Temporal and Kafka for Resilient Distributed Systems
  • Agentic AI Has an Observability Blind Spot Nobody Is Talking About
  • Liquid Glass, Material 3, and a Lot of Plumbing
  • How to Submit a Post to DZone
  1. DZone
  2. Coding
  3. Languages
  4. Enums in Python

Enums in Python

Here, learn how enums in Python provide a clear and expressive way to represent fixed sets of values, enhance code readability, prevent bugs, and more!

By 
Sameer Shukla user avatar
Sameer Shukla
DZone Core CORE ·
Jan. 10, 24 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
2.5K Views

Join the DZone community and get the full member experience.

Join For Free

Enumerations (enums), provide a powerful way to represent a set of named values. Enums bring clarity, maintainability, and readability to code by replacing numeric or string constants with more meaningful named constants.

Creating Enums

The more systematic of creating enums in Python is by using the Enum class from the enum module. For example: 

Python
 
from enum import Enum

class UserRole(Enum):
    ADMIN = "admin"
    USER = "user"
    EDITOR = "editor"
    GUEST = "guest"


In the example, UserRole is an enum with 4 members: ADMIN, USER, EDITOR, and GUEST, each associated with a string value. 

Enums guarantee unique names by design. Each member of an enum is distinct and attempting to create multiple members with the same name will raise a TypeError.

Python
 
from enum import Enum

class UserRole(Enum):
    ADMIN = "admin"
    USER = "user"
    EDITOR = "editor"
    GUEST = "guest"
    ADMIN = "super user"

#TypeError: Attempted to reuse key: 'ADMIN'


The enum module also has a decorator, @unique, that ensures that the enum should have a distinct name and values.

Python
 
from enum import Enum, unique

@unique
class UserRole(Enum):
    ADMIN = "admin"
    USER = "user"
    EDITOR = "editor"
    GUEST = "guest"
    ADMIN = "super user"

# TypeError: Attempted to reuse key: 'ADMIN'


If the enum has duplicate values, then the @unique decorator generates a ValueError.

Python
 
from enum import Enum, unique

@unique
class UserRole(Enum):
    ADMIN = "admin"
    USER = "user"
    EDITOR = "editor"
    GUEST = "guest"
    S_ADMIN = "admin"

# ValueError: duplicate values found in <enum 'UserRole'>: S_ADMIN -> ADMIN


Accessing Enum Members

We can access the name and value of an enum member as attributes. 

Python
 
print(UserRole.EDITOR)          # UserRole.EDITOR
print(UserRole.EDITOR.value)    # Editor
print(UserRole['ADMIN'])	    # UserRole.ADMIN


Iterating Over Enums

Iterating over enums is straightforward: the loop will output each member of the UserRole enum. 

Python
 
for role in UserRole:
    print(f"Role Name: {role.name} - Value: {role.value}")


Enum Comparisons

Enums supports comparisons both for equality and identity. For example:

Python
 
print(UserRole.ADMIN == UserRole.ADMIN) # True
print(UserRole.ADMIN is UserRole.ADMIN).  # True. 


Enums in Function Signatures

From Python's latest versions, 3.10 and above, enums can be used directly in function signatures. This allows us to specify an enum as an argument or return type in function signatures. 

  • Argument type: Specify the enum type as the type of function parameter.
Python
 
def get_user_role(role: UserRole):
    return role.value


  • Return type: Enum can be used as the return type of the function.
Python
 
def my_function() -> UserRole:
    return UserRole.ADMIN


Advantages of Using Enums

Improved Code Readability

Enums provide meaningful names for values, making the code more self-explanatory. Instead of using numeric or string constants scattered throughout the code, enum members give descriptive names to the possible values. 

Enhanced Maintainability

Enums make code more maintainable by centralizing the definition of possible values. If changes are needed, you only need to modify the enum definition rather than searching and updating occurrences throughout the code.

Prevention of Invalid Values

Enums help prevent the use of invalid values by restricting the choices to the defined enum members. This reduces the chances of introducing bugs due to unexpected values. In our User role example, if we pass an invalid role value, the program raises a ValueError.

Python
 
role = UserRole('s_admin')
# ValueError: 's_admin' is not a valid UserRole

role = UserRole('admin')
# <UserRole.ADMIN: 'admin'>


Expressive Intent

Enums excel in revealing the intention of your code by providing names that distinctly reflect the purpose of the values. This feature contributes to making the code more expressive, enhancing its clarity. 

Conclusion

Enums in Python provide a clear and expressive way to represent fixed sets of values. They enhance code readability, prevent bugs by restricting variable values, and contribute to a more maintainable and self-documenting code.

Maintainability Python (language) Data Types

Opinions expressed by DZone contributors are their own.

Related

  • Python Async/Sync: Advanced Blocking Detection and Best Practices (Part 2)
  • Pydantic: Simplifying Data Validation in Python
  • Bridging Graphviz and Cytoscape.js for Interactive Graphs
  • Getting Started With Snowflake Snowpark ML: A Step-by-Step Guide

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook