[Python-ideas] Enums

M.-A. Lemburg mal at egenix.com
Fri Jul 29 11:37:19 CEST 2011


Paul Colomiets wrote:
> On Fri, Jul 29, 2011 at 6:11 AM, Guido van Rossum <guido at python.org> wrote:
>> On Thu, Jul 28, 2011 at 5:46 PM, Michael Foord <fuzzyman at gmail.com> wrote:
>>> I agree "named values" would get us *most* of the value, and would both be
>>> less contentious and provide a common building block for third party
>>> libraries to build interesting and perhaps esoteric uses on.
>>>
>>> I *like* grouped named values (I think they make nice apis to read and use
>>> when used appropriately), but hey-ho.
>>
>> These could be two separate features. Named values could be a building
>> block for a built-in Enum type -- the named value would take care of
>> the str() and repr(), while the Enum type would only have to take care
>> of the nice syntax ("class Color(Enum): red = 1; ...") and just create
>> a bunch of named values in the class scope. The metaclass would do
>> something like "for key in classdict: classdict[key] = namedvalue(key,
>> classdict[key])" (though it would probably have to skip certain keys).
>>
> 
> 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.

Just to make sure:

namedvalue() will just change the repr() of the value, not
the str() of it, right ?

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.

-- 
Marc-Andre Lemburg
eGenix.com

Professional Python Services directly from the Source  (#1, Jul 29 2011)
>>> Python/Zope Consulting and Support ...        http://www.egenix.com/
>>> mxODBC.Zope.Database.Adapter ...             http://zope.egenix.com/
>>> mxODBC, mxDateTime, mxTextTools ...        http://python.egenix.com/
________________________________________________________________________

::: Try our new mxODBC.Connect Python Database Interface for free ! ::::


   eGenix.com Software, Skills and Services GmbH  Pastor-Loeh-Str.48
    D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
           Registered at Amtsgericht Duesseldorf: HRB 46611
               http://www.egenix.com/company/contact/



More information about the Python-ideas mailing list