[Python-ideas] PEP for enum library type?

Barry Warsaw barry at python.org
Tue Feb 12 20:45:18 CET 2013


On Feb 12, 2013, at 09:07 AM, Ethan Furman wrote:

>The result of the implementation I've been playing with looks something like:
>
>class Color(Enum):
>     type = 'sequence'
>     RED, GREEN, BLUE
>
>class Geometry(Enum):
>     type = 'unique'
>     LEFT, RIGHT, TOP, BOTTOM
>     WEST, EAST, NORTH, SOUTH
>
>class FieldType(Enum):
>     type = 'flag'
>     BINARY, AUTOINC, NULLABLE

IMHO, none of these approach the simplicity and explicitness of this API:

class Color(Enum):
    RED = 1
    GREEN = 2
    BLUE = 3

class FieldType(Enum):
    BINARY = 1
    AUTOINC = 2
    NULLABLE = 4

The savings in a few characters, does not (again IMHO) make up for all the
magic and implicitness you have to guess at with the top versions.

I also don't find much value in the 'unique' style, which seem like they're
just another way to name string constants.  Here though, you could argue that
the DRY principle at least makes them interesting, since IIUC, the explicit
alternative would be

class Geometry:
    LEFT = 'left'
    RIGHT = 'right'
    TOP = 'top'
    # ... and so on ...

I just wouldn't call these "enums" though. ;)  Again, being a little more
explicit seems like a win, or at least not so bad.

Cheers,
-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-ideas/attachments/20130212/40520683/attachment.pgp>


More information about the Python-ideas mailing list