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?
Asked
Active
Viewed 1,924 times
1 Answers
4
I think 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
Or:
you can execute pip install StrEnum
and have this:
from strenum import StrEnum
class MyEnum(StrEnum):
choice1 = "choice1"
choice2 = "choice2"

Amin
- 2,605
- 2
- 7
- 15
-
note that the first option still has limitations. for example, you can't do `"choice1" in iter(MyEnum)`. i don't know how the second option works. – yasin_alm Jul 28 '23 at 10:13