In Python >= 3.6 there is an enum.auto()
function.
The auto()
function can be used like this:
import enum
from enum import auto
class Fruits(enum.Enum):
APPLE = auto()
ORANGE = auto()
BANANA = auto()
I want to use it, but i cannot be sure if i am on Python >= 3.6 so I implemented it like this:
import enum
try:
from enum import auto
except ImportError:
__my_enum_auto_id = 0
def auto() -> int:
global __my_enum_auto_id
i = __my_enum_auto_id
__my_enum_auto_id += 1
return i
class Fruits(enum.Enum):
APPLE = auto()
ORANGE = auto()
BANANA = auto()
It works afaik, but it seems ugly to me. Any suggestions on how to improove it/make it better? or is it ok?
Is there some side effect I may have overlooked?
i
serve? \$\endgroup\$