[Python-Dev] PEP 435 -- Adding an Enum type to the Python standard library
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,
>>> didn't see one.
>> If you allowed this, there would be no way to look up an enumeration item
>> 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:
wasp = 1
bee = 2
ant = 3
wsap = wasp
waps = 1
In the above, looking up by the value 1 would always return Insect.wasp.
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Python-Dev