[Python-Dev] enum discussion: can someone please summarize open issues?
barry at python.org
Fri May 3 03:52:19 CEST 2013
On May 03, 2013, at 11:06 AM, Nick Coghlan wrote:
>> 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.
Should this be allowed then?
as_int = 1
as_dict = 2
as_tuple = 3
I still don't get it why this is an issue though, or at least why this is
different than any other getattr on any other class, or even Enums. I mean,
you could do a getattr on any other class or instance with any random user
input and there's no guarantee you could pass it straight to int() or any
other conversion type. So you pretty much have to be prepared to capture
More information about the Python-Dev