[Python-Dev] PEP 435 -- Adding an Enum type to the Python standard library

Tim Delaney timothy.c.delaney at gmail.com
Sun Apr 21 14:18:38 CEST 2013


On 21 April 2013 21:02, Greg Ewing <greg.ewing at canterbury.ac.nz> wrote:

> Barry Warsaw wrote:
>
>> On Apr 13, 2013, at 12:51 PM, Steven D'Aprano wrote:
>>
>
>  class Insect(Enum):
>>>    wasp = wsap = 1
>>>    bee = 2
>>>    ant = 3
>>>
>>> What's the justification for this restriction? I have looked in the PEP,
>>> and
>>> didn't see one.
>>>
>>
>> If you allowed this, there would be no way to look up an enumeration item
>> by
>> value.  This is necessary for e.g. storing the value in a database.
>>
>
> Hm. What you really want there isn't two enum objects with
> the same value, but two names bound to the same enum object.
> Then looking it up by value would not be a problem.


If there were some way to identify the canonical name a lookup by value
would be unambiguous. If we have iteration in definition order, I'd say the
first defined name for a value should be the canonical name, and any other
name for the value should be considered an alias.

That would preclude the syntax above, but the following might be workable:

class Insect(Enum):
    wasp = 1
    bee = 2
    ant = 3

    # aliases
    wsap = wasp
    waps = 1

In the above, looking up by the value 1 would always return Insect.wasp.

Tim Delaney
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20130421/5eea5b71/attachment.html>


More information about the Python-Dev mailing list