Introduction
Enums (short for Enumerations) are a symbolic name for a set of values. They are used to create a collection of related constants, making code more readable and maintainable. Python provides the enum
module, which allows you to define your own enumerations.
Creating Enums
Enums are created using the Enum
class from the enum
module. Each member of an enumeration is defined as a class attribute.
Example
from enum import Enum
class Color(Enum):
RED = 1
GREEN = 2
BLUE = 3
# Accessing enum members
print(Color.RED)
print(Color.GREEN)
print(Color.BLUE)
# Accessing enum member values
print(Color.RED.value)
print(Color.GREEN.value)
print(Color.BLUE.value)
# Accessing enum member names
print(Color.RED.name)
print(Color.GREEN.name)
print(Color.BLUE.name)
Output
Color.RED
Color.GREEN
Color.BLUE
1
2
3
RED
GREEN
BLUE
Iterating Over Enums
You can iterate over the members of an enumeration using a for
loop.
Example
for color in Color:
print(color)
Output
Color.RED
Color.GREEN
Color.BLUE
Comparing Enums
Enum members can be compared using identity (is
) and equality (==
) operators.
Example
color1 = Color.RED
color2 = Color.GREEN
color3 = Color.RED
print(color1 == color2) # Output: False
print(color1 == color3) # Output: True
print(color1 is color3) # Output: True
Enum Methods
Enums come with several useful methods for working with enumerations.
Example
# Getting a member by name
color = Color['RED']
print(color) # Output: Color.RED
# Getting a member by value
color = Color(1)
print(color) # Output: Color.RED
Extending Enums
You can extend enums to add methods or properties.
Example
from enum import Enum
class Color(Enum):
RED = 1
GREEN = 2
BLUE = 3
def describe(self):
return f"This is {self.name.lower()}."
# Using the method
color = Color.RED
print(color.describe()) # Output: This is red.
Auto Numbering Enums
You can use the auto()
function to automatically assign values to enum members.
Example
from enum import Enum, auto
class Color(Enum):
RED = auto()
GREEN = auto()
BLUE = auto()
# Accessing enum members
print(Color.RED)
print(Color.GREEN)
print(Color.BLUE)
# Accessing enum member values
print(Color.RED.value)
print(Color.GREEN.value)
print(Color.BLUE.value)
Output
Color.RED
Color.GREEN
Color.BLUE
1
2
3
Advanced Enum Features
Enum with Multiple Values
Enums can also be created with multiple values for each member.
Example
from enum import Enum
class Status(Enum):
ACTIVE = (1, 'Active')
INACTIVE = (2, 'Inactive')
PENDING = (3, 'Pending')
def __init__(self, code, description):
self.code = code
self.description = description
# Accessing multiple values
print(Status.ACTIVE.code) # Output: 1
print(Status.ACTIVE.description) # Output: Active
Using Enum in a Dictionary
Enums can be used as keys in dictionaries, which can be useful for configuration and lookup tables.
Example
settings = {
Status.ACTIVE: "The user is active.",
Status.INACTIVE: "The user is inactive.",
Status.PENDING: "The user's account is pending activation."
}
print(settings[Status.ACTIVE]) # Output: The user is active.
Flag Enums
The Flag
class (a variant of Enum
) is used for creating enums that represent bit flags. This is useful for bitwise operations.
Example
from enum import Flag, auto
class Permission(Flag):
READ = auto()
WRITE = auto()
EXECUTE = auto()
# Combining flags
permission = Permission.READ | Permission.WRITE
print(permission) # Output: Permission.READ|WRITE
# Checking flags
print(Permission.READ in permission) # Output: True
print(Permission.EXECUTE in permission) # Output: False
Conclusion
Enums in Python provide a way to define symbolic names for a set of values, making your code more readable and maintainable. The enum
module offers a variety of features, such as creating simple enums, auto-numbering enums, extending enums with methods, and using flags for bitwise operations. Understanding and utilizing enums can significantly improve the clarity and quality of your code.