[Python-ideas] PEP for enum library type?

Guido van Rossum guido at python.org
Tue Feb 12 20:59:54 CET 2013


After all the defenses I still don't like Tim's proposed syntax. Color me Barry.

On Tue, Feb 12, 2013 at 11:54 AM, Tim Delaney <tim.delaney at aptare.com> wrote:
> On 13 February 2013 06:13, Guido van Rossum <guido at python.org> wrote:
>>
>> So with Tim's implementation, what happens here:
>>
>> class Color(Enum):
>>   RED, GREEN, BLUE
>>   if sys.platform == 'win32':
>>     MAGENTA
>>
>> and you happen to have no "import sys" in your module? The sys lookup
>> will succeed, create a new enum, and then you will get an error
>> something like this:
>>
>> AttributeError: 'Enum' object has no attribute 'platform'
>
>
> Well, that particular case would work (you'd get a NameError: sys) due to
> having done an attribute lookup on sys, but the following:
>
> class Color(Enum):
>     RED, GREEN, BLUE
>     if platfor == 'win32':
>         MAGENTA
>
> would create an enum value 'platfor'.
>
> Personally, I don't think it's possible to have an enum with a simple
> interface and powerful semantics with just python code without it being
> fragile. I think I've got it fairly close, but there is horrible magic in
> there (multiple kinds) and there are definitely still edge cases. Any
> complete enum implementation is going to need some special handling by the
> parser I think.
>
> I'm actually thinking that to simplify things, I need a sentinel object to
> mark the end of the enum list (which allows other names after it). But that
> still wouldn't handle the case above (the if statement).
>
> BTW, for anyone who hasn't seen the magic code (largely uncommented, no
> explanations yet of how it's doing it - I probably won't get to that until
> the weekend) it's here: https://bitbucket.org/magao/enum
>
> Tim Delaney



--
--Guido van Rossum (python.org/~guido)



More information about the Python-ideas mailing list