[Python-Dev] enum discussion: can someone please summarize open issues?

Nick Coghlan ncoghlan at gmail.com
Fri May 3 03:06:40 CEST 2013


On Fri, May 3, 2013 at 9:57 AM, Ethan Furman <ethan at stoneleaf.us> wrote:
> On 05/02/2013 04:43 PM, Greg Ewing wrote:
>>
>> Guido van Rossum wrote:
>>>
>>> you should do some other check,
>>> e.g. "if x in Color:".
>>
>>
>> So you don't think it's important to have an easy
>> way to take user input that's supposed to be a
>> Color name and either return a Color or raise
>> a ValueError?
>
>
> I don't believe that's what he said:
>
>> The name lookup is only relevant if you already know that you have a
>> valid name of an enum in the class [...]
>
>
> User input should qualify, and using getattr(EnumClass, user_input) will get
> you an AttributeError instead of a ValueError if user_input is not valid,
> but surely you don't mind that small difference.  ;)

>>> int(getattr(C(), "__str__"))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: int() argument must be a string or a number, not 'method-wrapper'

That's the problem Greg is complaining about: when you use getattr to
do the name->enum member conversion, you have to do your own checking
to exclude method names.

This is part of why I think enums should offer an "as_dict()" method
that returns an ordered dictionary.

Cheers,
Nick.

--
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia


More information about the Python-Dev mailing list