[Python-Dev] constant/enum type in stdlib
Barry Warsaw
barry at python.org
Tue Nov 23 17:27:03 CET 2010
On Nov 23, 2010, at 01:50 PM, Michael Foord wrote:
>Right. As it happens I just submitted a patch to Barry Warsaw's enum package
>(nice), flufl.enum [1], to allow namedtuple style creation of named
>constants:
Thanks for the plug (and the nice patch).
FWIW, the documentation for the package is here:
http://packages.python.org/flufl.enum/
I made some explicit decisions about the API and semantics of this package, to
fit my own use cases and sensibilities. I guess you wouldn't expect anything
else <wink>, but I'm willing to acknowledge that others would make different
decisions, and certainly the number of existing enum implementations out there
proves that there are lots of interesting ways to go about it.
That said, there are several things I like about my package:
* Enums are not subclassed from ints or strs. They are a distinct data type
that can be converted to and from ints and strs. EIBTI.
* The typical way to create them is through a simple, but explicit class
definition. I personally like being explicit about the item values, and the
assignments are required to make the metaclass work properly, but Michael's
convenience patch is totally appropriate for cases where you don't care, or
you want a one-liner.
* Enum items are singletons and are intended to be compared by identity. They
can be compared by equality but are not ordered.
* Enum items have an unambiguous symbolic repr and a nice human readable str.
* Given an enum item, you can get to its enum class, and given the class you
can get to the set of items.
* Enums can be subclassed (though all items in the subclass must have unique
values).
In any case it may be that enums are too tied to specific use cases to find a
good common ground for the stdlib. I've been using my module for years and if
there's interest I would of course be happy to donate it for use in the
stdlib. Like the original sets implementation, it makes perfect sense to
provide them in a separate module rather than as a built-in type.
-Barry
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 836 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-dev/attachments/20101123/0d891968/attachment.pgp>
More information about the Python-Dev
mailing list