[Python-ideas] adopt an enum type for the standard library?

Raymond Hettinger python at rcn.com
Sat Apr 12 23:23:24 CEST 2008


> http://twoday.tuwien.ac.at/pub/files/enum
>
> I don't know how good this is, though.

It's not needed.  The namedtuple() function already makes enums trivially simple for anyone who wants to go down this path:

  >>> flags = namedtuple('flags', 'OK ERROR OTHER')(*range(3))
  >>> flags
  flags(OK=0, ERROR=1, OTHER=2)
  >>> flags.OTHER
  2

I'm strongly against adding any variant of enum() to the standard library.  It distracts users from existing, better approaches like 
module level constants, classes, dicts, and function attributes. While enums make some sense in compiled languages, there is almost 
zero need for them in Python.  Module level constants are often the way to go -- they look cleaner and they save an unnecessary 
level of indirection (that is cost-free in a compiled language but expensive in Python):

   re.IGNORECASE   is better than   re.flags.IGNORECASE
   decimal.Overflow   is better than   decimal.exceptions.Overflow


Raymond






More information about the Python-ideas mailing list