Python 3.4 Enum Backport (enum34)
`enum34` backports the `enum.Enum` class from Python 3.4 to older Python versions (2.4-3.3), enabling the use of enumerated types across diverse legacy environments. It also includes selected features from newer Python `enum` modules, such as `auto()` and `Flag` from Python 3.6. The library is currently at version 1.1.10, and due to its nature as a backport, active development has ceased, though it remains functional and stable for its intended purpose.
Warnings
- breaking `enum34` should NOT be used on Python 3.4 or newer. Python 3.4 introduced the `enum` module into the standard library. Using `enum34` on these versions can lead to conflicts, unexpected behavior, or unnecessary overhead. Always use the built-in `enum` module for Python 3.4+.
- gotcha While `enum34` primarily backports `Enum` from Python 3.4, it also includes features like `auto()`, `Flag`, and `StrEnum` which were introduced in later Python versions (3.6 and 3.11 respectively). Be aware that the behavior of these backported features might have subtle differences or limitations compared to their native implementations in newer Python versions.
- deprecated For all new Python projects or projects targeting Python 3.4 or newer, `enum34` is obsolete. Its sole purpose is to provide `enum` functionality to legacy Python 2 and early Python 3 environments (up to 3.3).
Install
-
pip install enum34
Imports
- Enum
from enum34 import Enum
- IntEnum
from enum34 import IntEnum
- auto
from enum34 import auto
Quickstart
from enum34 import Enum, auto
class Color(Enum):
RED = auto()
GREEN = auto()
BLUE = auto()
def __str__(self):
return self.name.lower()
print(f"First color: {Color.RED}")
print(f"Value of GREEN: {Color.GREEN.value}")
print(f"Access by value (1): {Color(1)}")
print(f"Iterating through colors:")
for color in Color:
print(f"- {color.name}: {color.value}")