[Python-ideas] adopt an enum type for the standard library?
mark at qtrac.eu
Wed Jan 23 16:12:41 CET 2008
On 2008-01-23, Tal Einat wrote:
> Christian Heimes wrote:
> > Mark Summerfield wrote:
> > > There is an enum module in PyPI
> -1 on adding a specific construct for enums.
> What I usually do in Python is this:
> ERROR, OK, ALSO_OK = range(-1, -1 + 3)
> Start and stop values, as well step sizes other than one, are easily
> achieved. Skipping values is done like this:
> ERROR, OK, ALSO_OK, SOMEOTHER, LAST = range(-1, -1 + 3) + range(1000, 1000
> + 2)
> Updating the range(s) appropriately when adding/changing values is
> easy enough. I find this solution to be good enough since the values
> are not ever meant to be used explicitly, and values are not
> added/changed often.
> This is perhaps not very pretty or concise, but it's simple and it
> only uses Python syntax and constructs which everyone knows and
> understands. Readability and simplicity are usually my top concerns,
> so this fits the bill.
Unfortunately, the "const-ness" of enums defined this way is merely
conventional, so it is easy to change the value of one by mistake.
Using namedtuples means that if you try to assign to the thing you at
least get an exception raised. Not that I'm particularly in favour of
using namedtuples for this purpose, but they are the only "convenient"
way to have enums based on the standard library that I know of.
Although I think that enums are a fundamental (as are sets, and yet it
took many years for them to arrive in the standard library), at least
putting enums in the standard library would allow those who want them to
use them out of the box without impinging on those who are happy with
not having them.
As for which enum implementation, whether the one from PyPI or one from
the cookbook, or another one entirely, I have no strong feelings.
Mark Summerfield, Qtrac Ltd., www.qtrac.eu
More information about the Python-ideas