[Python-ideas] Macro-Powered Enums

Haoyi Li haoyi.sg at gmail.com
Sat Jul 6 02:56:11 CEST 2013


Hey All,

I know this ship has already sailed with PEP 435, but I've been working on
a prototype implementation of enums using MacroPy
macros<https://github.com/lihaoyi/macropy#enums>
.

The goal was to have enums whose methods provide most of the nice
capabilities of int enums (auto-generated indexes, fast comparison,
incrementing, index-arithmetic, find-by-index) and string enums (nice
__repr__, find-by-name) but has the ability to smoothly scale to
full-fledged objects with methods *and* fields, all this is an extremely
concise way.

The gimmick here is that it uses macros to provide a concise syntax to
allow you to construct each enum instance with whatever constructor
parameters you wish, java-enum-style
(here<https://github.com/lihaoyi/macropy#complex-enums>).
This allows a degree of enums-as-objects which i haven't seen in any other
library (most don't allow custom fields).

Probably not standard-library worthy, but I thought it was pretty cool.

-Haoyi
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20130706/dc6c4f9b/attachment.html>


More information about the Python-ideas mailing list