The enum.Enum
class in Python’s enum
module allows you to create enumerations, which are a set of symbolic names bound to unique, constant values. Enumerations can be used to represent fixed sets of values, making your code more readable and maintainable.
Table of Contents
- Introduction
enum.Enum
Class Syntax- Examples
- Basic Usage
- Iterating Over Enum Members
- Accessing Enum Members and Their Values
- Comparison of Enum Members
- Using Enums in Functions
- Real-World Use Case
- Conclusion
Introduction
Enumerations, created using the enum.Enum
class, provide a way to define symbolic names for a set of values. Each member of an enumeration has a name and a value, making the code more readable and easier to maintain.
enum.Enum Class Syntax
Here is how you define an enumeration with the enum.Enum
class:
from enum import Enum
class EnumName(Enum):
MEMBER1 = value1
MEMBER2 = value2
MEMBER3 = value3
Parameters:
EnumName
: The name of the enumeration.MEMBER
: The symbolic name of the enumeration member.value
: The constant value associated with the enumeration member.
Examples
Basic Usage
Define a simple enumeration for days of the week.
Example
from enum import Enum
class Day(Enum):
SUNDAY = 1
MONDAY = 2
TUESDAY = 3
WEDNESDAY = 4
THURSDAY = 5
FRIDAY = 6
SATURDAY = 7
print(Day.MONDAY)
print(Day.MONDAY.name)
print(Day.MONDAY.value)
Output:
Day.MONDAY
MONDAY
2
Iterating Over Enum Members
Iterate over the members of an enumeration.
Example
from enum import Enum
class Color(Enum):
RED = 1
GREEN = 2
BLUE = 3
for color in Color:
print(color)
Output:
Color.RED
Color.GREEN
Color.BLUE
Accessing Enum Members and Their Values
Access the members and values of an enumeration.
Example
from enum import Enum
class Status(Enum):
ACTIVE = 1
INACTIVE = 0
status = Status.ACTIVE
print(f"Status: {status.name}, Value: {status.value}")
Output:
Status: ACTIVE, Value: 1
Comparison of Enum Members
Compare members of an enumeration.
Example
from enum import Enum
class Priority(Enum):
LOW = 1
MEDIUM = 2
HIGH = 3
priority = Priority.HIGH
if priority == Priority.HIGH:
print("High priority")
else:
print("Not high priority")
Output:
High priority
Using Enums in Functions
Use enumerations as function arguments.
Example
from enum import Enum
class Direction(Enum):
NORTH = 1
SOUTH = 2
EAST = 3
WEST = 4
def move(direction):
if direction == Direction.NORTH:
print("Moving north")
elif direction == Direction.SOUTH:
print("Moving south")
elif direction == Direction.EAST:
print("Moving east")
elif direction == Direction.WEST:
print("Moving west")
move(Direction.NORTH)
move(Direction.WEST)
Output:
Moving north
Moving west
Real-World Use Case
Representing User Roles
Use an enumeration to represent different user roles in an application.
Example
from enum import Enum
class UserRole(Enum):
ADMIN = 1
EDITOR = 2
VIEWER = 3
def get_permissions(role):
if role == UserRole.ADMIN:
return "Full access"
elif role == UserRole.EDITOR:
return "Edit access"
elif role == UserRole.VIEWER:
return "View only access"
print(get_permissions(UserRole.ADMIN))
print(get_permissions(UserRole.EDITOR))
print(get_permissions(UserRole.VIEWER))
Output:
Full access
Edit access
View only access
Conclusion
The enum.Enum
class is used for creating enumerations in Python. It allows you to define a set of symbolic names for constant values, improving the readability and maintainability of your code. Enumerations can be used in various scenarios, such as representing user roles, statuses, and directions, making your code more structured and understandable.