Weekly edition Kernel Security Distributions Contact Us Search Archives Calendar Subscribe Write for LWN LWN.net FAQ Sponsors

# An "enum" for Python 3

## An "enum" for Python 3

Posted May 22, 2013 23:35 UTC (Wed) by marcH (subscriber, #57642)
Parent article: An "enum" for Python 3

> In addition, there is a way to avoid having to assign values for each enum member (auto-numbering, essentially), but its syntax is clunky.

Wait... so enums are not integers, yet it's difficult not to give them each a number!? I must have missed something.

An "enum" for Python 3

Posted May 23, 2013 5:14 UTC (Thu) by gdt (guest, #6284) [Link]

Enums are numbered, but are not integers. Numbers need not support the complete set of arithmetic operations.

As a everyday example of this, counting numbers (1, 2, 3, ...) are incomplete for the subtraction operator. Try to represent 1 - 2 as a counting number.

Some of the questions in the mailing list were around what arithmetic properties a enum has: for example, are they ordered (ie, red < green). Since maths doesn't care for enums much (and so doesn't define them with rigor), the real question was what properties should a enum have to be useful for programming. For example, it was widely felt that (red + green) should not evaluate as it indicates a programming error.

Enums might be implemented using integers. But one can say the same for sets. The whole point of a programming language is to develop useful abstractions to simplify the programming task, otherwise it may as well be assembler.

An "enum" for Python 3

Posted May 23, 2013 7:31 UTC (Thu) by micka (subscriber, #38720) [Link]

Yet I don't understand why they must still be numbered.
That they are numbered as an implementation detail doesn't shock me, but should the numbered aspect be present in the language, event in the most common way to define them ?

An "enum" for Python 3

Posted May 23, 2013 8:16 UTC (Thu) by jezuch (subscriber, #52988) [Link]

> Yet I don't understand why they must still be numbered.

My thought as well. At this point it looks like it's a calque of enums from C (i would say "mindless" but it *was* discussed to death, after all ;) ). Having lived with enums in Java (which I find, frankly, rather excellent), I think it's a huge lost opportunity.

An "enum" for Python 3

Posted May 23, 2013 16:06 UTC (Thu) by pboddie (guest, #50784) [Link]

I don't track Java development actively, so it is nice to see that the original way of declaring enumerations has been deprecated. It was one of the ways that "classic Java", in contrast to the initial and sustained hype around the language, was let down by a number of unjustifiably weak features: a bit like taking a tour of an alien spacecraft from the future and finding out that the on-board sanitation is a bucket by the captain's seat.

The problem with introducing something similar in Python and having it as elegant as one might wish for is the unfortunate aversion amongst the core developers to changing the language syntax, even if that is really the best or most logical path to take in order to be able to express the new feature. So we end up with the risk that there is a shoe-horning of features into existing constructs that will probably serve to unnecessarily confuse people in future.

I suppose that one might look at the way it has been done and to consider more general mechanisms. In effect, values in PEP 435 enumerations can be regarded as predefined instances of the enumeration class, which I think is not always logical, since a colour is not an instance of a set of colours but rather a member of that set, although such a distinction is not always informative (integers are instances of the int type which can more or less be considered as the set of all integers even though it isn't really such a thing). However, this does allow us to consider using a similar shorthand elsewhere to define restricted collections of instances for a class.

What was noted in Andrew Cooke's response to the PEP is that the issue of introducing atoms/symbols in Python has been avoided once again. This would have been a particularly appropriate occasion to consider that matter.

An "enum" for Python 3

Posted May 23, 2013 8:33 UTC (Thu) by nix (subscriber, #2304) [Link]

Because most uses for enums eventually require that they be converted to a number for communication to some external thing which is representing them as (say) a bunch of C #defines, or, indeed, a C enum (or indeed any of a number of things in the existing standard library representing numbered things like error codes out there on the Internet). You could make enums an intrinsically unnumbered type, or a type whose numbering is hidden and could not be specified at all, but it would drastically limit their usefulness, since they would no longer be interconvertible with any other language's 'enum', and you'd need to write conversion wrappers all over the place.

An "enum" for Python 3

Posted May 24, 2013 7:49 UTC (Fri) by jezuch (subscriber, #52988) [Link]

> You could make enums an intrinsically unnumbered type, or a type whose numbering is hidden and could not be specified at all, but it would drastically limit their usefulness

Or you could allow enums to have members - fields and methods - and provide the conversion yourself when needed. You could also add some syntactic sugar for the common case. That's one of the great things about enums in Java - they are almost-regular classes, so they can engage in (interface) inheritance and polymorphism at will. Oh, and you can switch() on them ;)

An "enum" for Python 3

Posted May 23, 2013 8:34 UTC (Thu) by nix (subscriber, #2304) [Link]

Also... it's in the name. Enumerated types, are, well, enumerated. That means they are like, well, numbers. If they weren't like numbers it would be pretty silly to call them enumerated. :)

An "enum" for Python 3

Posted May 23, 2013 8:58 UTC (Thu) by micka (subscriber, #38720) [Link]

Ah, I admit I forgot about the etymology of the word enumeration.
Actually, for me, enumerate is more "give all elements one after the other" without any idea of counting or order, as it is in my native language (french), so that could have influenced the way I define the notion of enum.

An "enum" for Python 3

Posted May 23, 2013 9:37 UTC (Thu) by nix (subscriber, #2304) [Link]

Yeah, the link between 'enumerate' and 'number' is all the way back in Latin ("count out"). The fossil is still there in the word clearly enough for a native speaker to notice, though of course etymology is not destiny and there are *lots* of words whose etymology is a totally misleading guide to their meaning.

An "enum" for Python 3

Posted May 23, 2013 10:31 UTC (Thu) by ballombe (subscriber, #9523) [Link]

See <http://en.wikipedia.org/wiki/Ordinal_arithmetic> for one of the mathematical views on the subject.