On 21 April 2013 21:02, Greg Ewing <[email protected]> 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
_______________________________________________
Python-Dev mailing list
[email protected]
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe:
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com