Pythonic enums with better string support
June 23, 2022
Enum's in Python were introduced in PEP 435.
class BRAIN(str, Enum):
SMALL = 'small'
MEDIUM = 'medium'
GALAXY = 'galaxy'
def __str__(self) -> str:
return str.__str__(self)
- avoids the need to use
.value
anywhere at all in your code - enum values duck type to strings in the ways you’d expect
- you can iterate over brain and get string-likes out
- random.choice() is broken
- type hints work