[Python-ideas] PEP for enum library type?

Ethan Furman ethan at stoneleaf.us
Tue Feb 12 20:58:30 CET 2013


On 02/12/2013 11:45 AM, Barry Warsaw wrote:
> 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.

0)  Once you read the docs you don't have to guess  ;)
1)  Magic is fun.  :)
2)  I hate repetition  (= 1  = 2  = 3 is repetitive)
3)  I make mistakes when repeating stuff (... = 11   = 11  = 13)


> 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 ...

Class membership can also be an advantage.

--
~Ethan~



More information about the Python-ideas mailing list