The enum package in python 3.11 has the StrEnum class. I consider it very convenient but cannot use it in python 3.10. What would be the easiest method to use this class anyway?
On Python 3.10, you can inherit from str
and Enum
to have a StrEnum
:
from enum import Enum
class MyEnum(str, Enum):
choice1 = "choice1"
choice2 = "choice2"
With this approach, you have string comparison:
"choice1" == MyEnum.choice1
>> True
Be aware, however, that Python 3.11 makes a breaking change to classes which inherit from (str, Enum): https://github.com/python/cpython/issues/100458. You will have to change all instances of (str, Enum) to StrEnum when upgrading to maintain the same behavior.
Or:
you can execute pip install StrEnum
and have this:
from strenum import StrEnum
class MyEnum(StrEnum):
choice1 = "choice1"
choice2 = "choice2"