[Python-Dev] AutoNumber Enum
Brett Cannon
brett at python.org
Wed Jun 29 14:15:11 EDT 2016
On Wed, 29 Jun 2016 at 10:41 Ethan Furman <ethan at stoneleaf.us> wrote:
> 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?
>
Is it going to subclass Enum or IntEnum? Personally I would be quite happy
to never have to specify a value for enums ever again, but only if they
subclass Enum (since IntEnum is for compatibility with C stuff where a
specific value is needed I don't think users need to mess that up by having
the automatic numbering not work how they would expect).
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20160629/70490d9e/attachment.html>
More information about the Python-Dev
mailing list