Reimplementing Python's enum.auto() for compatibility 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() -&gt; 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?
The easiest solution would be to use the aenum1 library, which is drop-in compatible with the stdlib and also has some advanced features not found in the stdlib.  It also works in Python versions 2.7 and 3.3+.



1 Disclosure:  I am the author of the Python stdlib Enum, the enum34 backport, and the Advanced Enumeration (aenum)  library.