[Python-ideas] Enums

Matt Joiner anacrolix at gmail.com
Fri Jul 29 02:56:08 CEST 2011


I agree with Steven,

At first the thought of a standardized enum sounded great. Enums are
named constants, this is something Python can already give us.
Sometimes the enum values are such that they can be treated as flags.
This is a property of integers. In most languages enums are integer
constants, because those languages are so much closer to the machine.

However Python is a runtime, and it's not Pythonic to attempt to
enforce values to be constant, let require that they be integers.
Furthermore, it's dangerous to suggest that this be commonplace by
adding it to the standard library, when superior Pythonic alternatives
exist. If you remove the constraint that enums necessarily correspond
to integers, suddenly there is no point: enums for the sake of
compatibility and debugging C interfaces, where not even C has that
feature built in.

While out of tradition, enum like values are still done with integers
even in pure Python programs, it seems to me the proper approach is
something like:

Colors = {'red', 'blue', 'green'}
color = 'red'

OpenFlags = {'append', 'binary', 'exclusive', 'read', }

py_open('/some/path', {'binary', 'exclusive', 'read', 'write'})

Adding enums to the standard library is going to cause a rush to
rewrite everything to use enums, where perfectly good alternatives are
already in use.

I think it should just be accepted that C interfaces work with
integers and move on.

On Fri, Jul 29, 2011 at 7:16 AM, Raymond Hettinger
<raymond.hettinger at gmail.com> wrote:
> Wikipedia has a nice survey and comparison of enums in other languages:
> http://en.wikipedia.org/wiki/Enumerated_type
>
> Raymond
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> http://mail.python.org/mailman/listinfo/python-ideas
>
>



More information about the Python-ideas mailing list