[Python-ideas] Enums

Nick Coghlan ncoghlan at gmail.com
Fri Jul 29 16:55:32 CEST 2011


On Fri, Jul 29, 2011 at 7:37 PM, M.-A. Lemburg <mal at egenix.com> wrote:
> Paul Colomiets wrote:
>> The problem with named value is that:
>>
>>     namedvalue(1, "red")
>>
>> Will give you are repr of "red". And you don't know whether it's
>> TerminalColors.red or WebSafe.red or BuildbotState.red. And
>> most of us will be too lazy to add group name or module name
>> directly into the named value (partially because it's repeating
>> yourself).
>>
>> So the big feature of flufl.enum is classname (call it group
>> name) in repr and isinstance check.
>
> A meta class variant could easily add class and module
> names to the attrbute name, if needed/wanted.

Given that the status quo is for "TerminalColors.red" et al to just
display as "1" (or whatever integers they're assigned), I'm finding it
hard to consider this a particularly significant criticism. If we
don't even know what *kind* of values are being passed to a debugging
message, we have bigger problems than whether or not those values have
been given names (and searching for all named values of 'red' with a
value of '1' is going to create a much smaller resulting set than
searching for all values of '1' ).

> Just to make sure:
>
> namedvalue() will just change the repr() of the value, not
> the str() of it, right ?

Yeah, I figured out the mechanics needed to make that work properly
when writing up the recipe for the cookbook
(http://code.activestate.com/recipes/577810-named-values/)

> I think that's essential for making namedvalue()s useful
> in practice, since you mostly need this for debugging
> and error reporting and don't want the namevalue aspect
> of a constant to get in the way of its normal use in
> e.g. conversion to text formats such as JSON, XML, etc.

Yeah, the case I first thought of was writing numbers to CSV files,
but it's really any data export operation.

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia



More information about the Python-ideas mailing list