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

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Low-Code Development: Leverage low and no code to streamline your workflow so that you can focus on higher priorities.

DZone Security Research: Tell us your top security strategies in 2024, influence our research, and enter for a chance to win $!

Launch your software development career: Dive head first into the SDLC and learn how to build high-quality software and teams.

Open Source Migration Practices and Patterns: Explore key traits of migrating open-source software and its impact on software development.

Related

  • Applying the Pareto Principle To Learn a New Programming Language
  • BigQuery DataFrames in Python
  • Python Context Managers Simplified
  • Exploring Python Generators

Trending

  • Efficient Data Management With Offset and Cursor-Based Pagination in Modern Applications
  • Transforming Software Development With Low-Code and No-Code Integration
  • From Backlog Manager to Product Manager [Video]
  • Javac and Java Katas, Part 1: Class Path
  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
Like (1)
Save
Tweet
Share
1.4K 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

  • Applying the Pareto Principle To Learn a New Programming Language
  • BigQuery DataFrames in Python
  • Python Context Managers Simplified
  • Exploring Python Generators

Partner Resources


Comments

ABOUT US

  • About DZone
  • Send feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: