[Python-Dev] PEP 435: initial values must be specified? Yes (original) (raw)

Tim Delaney timothy.c.delaney at gmail.com
Sun May 5 23:55:22 CEST 2013


On 6 May 2013 06:09, Ethan Furman <ethan at stoneleaf.us> wrote:

On 05/05/2013 10:07 AM, � wrote:> I'm chiming in late, but am I the only one who's really bothered by the syntax?

class Color(Enum): red = 1 green = 2 blue = 3 No, you are not only one that's bothered by it. I tried it without assignments until I discovered that bugs are way too easy to introduce. The problem is a successful name lookup looks just like a name failure, but of course no error is raised and no new enum item is created: --> class Color(Enum): ... red, green, blue ... --> class MoreColor(Color): ... red, orange, yellow ... --> type(MoreColor.red) is MoreColor False --> MoreColor.orange <MoreColor.orange: 4> # value should be 5

Actually, my implementation at https://bitbucket.org/magao/enum (the one mentioned in the PEP) does detect MoreColor.red as a duplicate. It's possible to do it, but it's definitely black magic and also involves use of sys._getframe() for more than just getting module name.

from enum import Enum class Color(Enum): ... red, green, blue ... class MoreColor(Color): ... red, orange, yellow ... Traceback (most recent call last): File "", line 1, in File ".\enum.py", line 388, in new raise AttributeError("Duplicate enum key '%s.%s' (overriding '%s')" % (result.name, v.key, k eys[v.key])) AttributeError: Duplicate enum key 'MoreColor.red' (overriding 'Color.red')

So long as I can get one of the requirements documented to implement an auto-number syntax I'll be happy enough with stdlib enums I think.

class Color(AutoIntEnum): red = ... green = ... blue = ...

Not as pretty, but ends up being less magical.

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



More information about the Python-Dev mailing list