[Python-Dev] PEP 435 - requesting pronouncement

Guido van Rossum guido at python.org
Sun May 5 19:49:22 CEST 2013


On Sun, May 5, 2013 at 10:41 AM, Paul Moore <p.f.moore at gmail.com> wrote:
> OK, I thought I'd take a look. I have never particularly needed enums in
> real life, so I'm reading the PEP from the POV of a naive user who is just
> thinking "hey, neat, Python got enums, let's see how they work". I have been
> skimming the discussions and my head has been exploding with the complexity,
> so I admit I was very, very scared that the PEP might be equally daunting.
>
> First, the good news - from the POV described above, the PEP is both
> readable and intuitive. Nice job, guys!
>
> Now the problems I had:
>
> 1. Having to enter the values is annoying. Sorry, I read the rationale and
> all that, and I *still* want to write a C-Like enum { A, B, C }. I fully
> expect to edit and reorder enums (if I ever use them) and get irritated with
> having to update the value assignments.

I guess there are cultural differences around this. Anyway, you can
use the functional/convenience API for this purpose.

> 2. Enums are not orderable by default. Yuk. I doubt I'll care about this
> often (iteration is more important) but when I do, I'll be annoyed.

I personally agree with you, but not strongly enough to override Barry
and Eli who seem to be strongly for unordered enums.

> 3. This is just a thought, but I suspect that IntEnums iterating in
> definition order but ordering by value could trip people up and cause hard
> to diagnose bugs.

This is somewhat in conflict with your #1. :-)

> 4. I'll either use the functional form all the time (because I don't have to
> specify values) or never (because it's ugly as sin). I can't work out which
> aspect will win yet.

But not everybody will make the same choice.

> And one omission that struck me. There's no mention of the common case of
> bitmap enums.
>
> class Example(Enum):
>    a = 1
>    b = 2
>    c = 4
>
> Do I need to use an IntEnum (given the various warnings in the PEP about how
> "most people won't need it") if I want to be able to do things like flags =
> "Example.a | Example.c"? I think there should at least be an extended
> example in the PEP covering a bitmap enum case. (And certainly the final
> documentation should include a cookbook-style example of bitmap enums).

You'd have to use IntEnum. Plus, these are hardly enums -- they are a
particularly obscure old school hack for representing sets of flags.
(I liked Pascal's solution for this better -- it had a bit set data
structure that supported sets of enums.)

> Summary - good job, I like the PEP a lot. But Python's enums are very unlike
> those of other languages, and I suspect that's going to be more of an issue
> than you'd hope...

We're pretty confident that we're doing about the best job possible
given the constraints (one of which is getting this accepted into
Python 3.4 without any of the participants incurring permanent brain
damage).

-- 
--Guido van Rossum (python.org/~guido)


More information about the Python-Dev mailing list