Skip to content

Python Enums

Enums (Enumerations) are a distinct data type consisting of a set of named values called members. In Python, enums are defined using the Enum class in the enum module. Enums are useful for representing a collection of related constants and making the code more readable and maintainable.

1. Defining Enums

Enums can be defined by subclassing the Enum class and defining class attributes.

Example:

from enum import Enum

class Color(Enum):
    RED = 1
    GREEN = 2
    BLUE = 3

print(Color.RED)
print(Color.GREEN)
print(Color.BLUE)

Output:

Color.RED
Color.GREEN
Color.BLUE

2. Accessing Enum Members

Enum members can be accessed by name or value.

Example:

from enum import Enum

class Color(Enum):
    RED = 1
    GREEN = 2
    BLUE = 3

# Access by name
print(Color.RED)

# Access by value
print(Color(2))

Output:

Color.RED
Color.GREEN

3. Iterating Over Enum Members

You can iterate over enum members using a for loop.

Example:

from enum import Enum

class Color(Enum):
    RED = 1
    GREEN = 2
    BLUE = 3

# Iterating over enum members
for color in Color:
    print(color)

Output:

Color.RED
Color.GREEN
Color.BLUE

4. Enum Member Attributes

Enum members have two main attributes: name and value.

Example:

from enum import Enum

class Color(Enum):
    RED = 1
    GREEN = 2
    BLUE = 3

# Accessing name and value attributes
print(Color.RED.name)
print(Color.RED.value)

Output:

RED
1

5. Comparing Enums

Enums can be compared using identity and equality operators.

Example:

from enum import Enum

class Color(Enum):
    RED = 1
    GREEN = 2
    BLUE = 3

# Comparing enums
print(Color.RED == Color.RED)
print(Color.RED == Color.GREEN)
print(Color.RED is Color.RED)
print(Color.RED is Color.GREEN)

Output:

True
False
True
False

6. Extending Enums

You can extend enums by defining additional methods and properties.

Example:

from enum import Enum

class Color(Enum):
    RED = 1
    GREEN = 2
    BLUE = 3

    def describe(self):
        return f"{self.name} is color number {self.value}"

print(Color.RED.describe())
print(Color.GREEN.describe())

Output:

RED is color number 1
GREEN is color number 2

7. Auto-Numbered Enums

The auto() function can be used to automatically assign values to enum members.

Example:

from enum import Enum, auto

class Color(Enum):
    RED = auto()
    GREEN = auto()
    BLUE = auto()

print(Color.RED.value)
print(Color.GREEN.value)
print(Color.BLUE.value)

Output:

1
2
3

Conclusion

Enums in Python are a powerful feature for creating readable and maintainable code. They allow you to define a set of named values, which can be accessed, iterated over, and compared easily. By practicing the examples provided, you can gain a deeper understanding of how to use enums effectively in your Python projects.