[Python-Dev] enum discussion: can someone please summarize open issues? (original) (raw)
Barry Warsaw barry at python.org
Fri May 3 03:52:19 CEST 2013
- Previous message: [Python-Dev] enum discussion: can someone please summarize open issues?
- Next message: [Python-Dev] enum discussion: can someone please summarize open issues?
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
On May 03, 2013, at 11:06 AM, Nick Coghlan wrote:
User input should qualify, and using getattr(EnumClass, userinput) will get you an AttributeError instead of a ValueError if userinput is not valid, but surely you don't mind that small difference. ;)
int(getattr(C(), "str")) Traceback (most recent call last): File "", line 1, in 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 "asdict()" method that returns an ordered dictionary.
Should this be allowed then?
class Transformations(Enum): 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 exceptions anyway.
-Barry
- Previous message: [Python-Dev] enum discussion: can someone please summarize open issues?
- Next message: [Python-Dev] enum discussion: can someone please summarize open issues?
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]