[Python-Dev] AutoNumber Enum

Guido van Rossum guido at python.org
Wed Jun 29 15:11:39 EDT 2016


And how would you implement that without support from the compiler?
Does it use a hook that catches the NameError?

On Wed, Jun 29, 2016 at 11:15 AM, Brett Cannon <brett at python.org> wrote:
>
>
> 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).
>
> _______________________________________________
> Python-Dev mailing list
> Python-Dev at python.org
> https://mail.python.org/mailman/listinfo/python-dev
> Unsubscribe:
> https://mail.python.org/mailman/options/python-dev/guido%40python.org
>



-- 
--Guido van Rossum (python.org/~guido)


More information about the Python-Dev mailing list