[Python-Dev] PEP 435 -- Adding an Enum type to the Python standard library

Ethan Furman ethan at stoneleaf.us
Sat Apr 27 19:04:10 CEST 2013

On 04/27/2013 09:01 AM, Guido van Rossum wrote:
> Hm... A lightbulb just went off. Objects representing both undecorated
> and decorated methods have a __get__() method, courtesy of the
> descriptor protocol. Maybe checking for that will work? It feels
> Pythonic to me: it uses a corner of the language that most people
> don't even know exists (*), but it produces the desired effect in
> almost all cases that matter, the pattern is simple to describe and
> easy to use without thinking about it, and for experts the rules are
> completely clear, uncomplicated, and free of heuristics, so it is
> possible to reason about corner cases.

While this will certainly work, it means you can't have class variables that happen to be the same type as the enum -- 
so no int in an IntEnum, for example.

The solution I like best is the helper class (called, originally enough, enum), and only those items get transformed:

class Planet(IntEnum):
     MERCURY = enum(1)
     VENUS = enum(2)
     EARTH = enum(3)
     rough_pi = 3     # not transformed


More information about the Python-Dev mailing list