[Python-Dev] constant/enum type in stdlib
Steven D'Aprano
steve at pearwood.info
Tue Nov 23 22:30:37 CET 2010
Antoine Pitrou wrote:
> Python already has an enumeration capability. It's called range().
> There's nothing else that C enums have. AFAICT, neither do enums in
> other mainstream languages (assuming they even exist; I don't remember
> Perl, PHP or Javascript having anything like that, but perhaps I'm
> mistaken).
In Pascal, enumerations are a type, and the value of the named values
are an implementation detail. E.g. one would define an enumerated type:
type
flavour = (sweet, salty, sour, bitter, umame);
var
x: flavour;
and then you would write something like:
x := sour;
Notice that the constants sweet etc. aren't explicitly predefined, since
they're purely internal details and the compiler is allowed to number
them any way it likes. In Python, we would need stronger guarantees
about the values chosen, so that they could be exposed to external
modules, pickled, etc.
But that doesn't mean we should be forced to specify the values ourselves.
--
Steven
More information about the Python-Dev
mailing list