[Python-Dev] AutoNumber Enum

Ethan Furman ethan at stoneleaf.us
Wed Jun 29 13:40:40 EDT 2016


There is a several-month-old request to add aenum's [1] AutoNumberEnum 
to the stdlib [2].

The requester and two of the three developers of Enum are in favor (the 
third hasn't chimed in yet).

This new addition would enable the following:

   from Enum import AutoNumberEnum

   class Color(AutoNumberEnum):
       # auto-number magic is on
       Red
       Green
       Blue
       Cyan
       # magic turns off when non-enum is defined

       def is_primary(self):
           # typos in methods, etc, will raise
           return self in (self.Red, self.Grene, self.Blue)

       # typos after the initial definition stanza will raise
       BlueGreen = Blue + Grene

There is, of course, the risk of typos during the initial member 
definition stanza, but since this magic only happens when the user 
explicitly asks for it (AutoNumberEnum), I think it is acceptable.

The `start` parameter is still available, and assigning a number is 
supported (subsequent numbers will (re)start from the assigned number).

Thoughts?  Opinions?  Flames?

--
~Ethan~



[1] https://pypi.python.org/pypi/aenum
[2] http://bugs.python.org/issue26988


More information about the Python-Dev mailing list