[Python-Dev] enum discussion: can someone please summarize open issues? (original) (raw)

Greg Ewing greg.ewing at canterbury.ac.nz
Sat May 4 01:15:17 CEST 2013


Eli Bendersky wrote:

I'm just curious what it is about enums that sets everyone on a "let's make things safer" path. Python is about duck typing, it's absolutely "unsafe" in the static typing sense, in the most fundamental ways imaginable.

This isn't about catching bugs in the program, it's about validating user input. That's a common enough task that it deserves to have a convenient way to do it correctly.

Imagine if int() had the property that, as well as accepting strings of decimal digits, it also accepted the string "guido" and returned his birthday as a DateTime object. When people complain, they're told it's okay, you only need to write

if s != "guido":
   x = int(s)
else:
   raise ValueError

What would you think of that situation?

Why is an Enum different than any other class?

It's not, that's the whole point. IMO it deserves to have a convenient way of mapping a valid string representation -- and nothing else -- to a valid value, just as much as any other type does.

-- Greg



More information about the Python-Dev mailing list