On 6 May 2013 06:09, Ethan Furman <et...@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 "<stdin>", line 1, in <module> 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
_______________________________________________ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com