[Python-ideas] Enums

Michael Foord fuzzyman at gmail.com
Thu Jul 28 15:00:02 CEST 2011


On 28 July 2011 13:17, Ezio Melotti <ezio.melotti at gmail.com> wrote:

> Hi,
>
>
> On 28/07/2011 4.12, Nick Coghlan wrote:
>
>> On Thu, Jul 28, 2011 at 10:56 AM, Barry Warsaw<barry at python.org>  wrote:
>>
>>> Again, looking at how I've used them extensively over the last several
>>> years,
>>> I would much rather write
>>>
>>>    class Colors(Enum):
>>>        red = 1
>>>        green = 2
>>>        blue = 3
>>>
>>> than
>>>
>>>    red = NamedValue('red', 1)
>>>    green = NamedValue('green', 2)
>>>    blue = NamedValue('blue', 3)
>>>
>>> To me, the duplication is jarring and error prone.
>>>
>> Yeah, I'd actually be inclined to define such values programmatically
>> rather than writing them out manually like that:
>>
>> _named_colours = dict(
>>   red=0xFF0000,
>>   green=0x00FF00,
>>   blue=0x0000FF,
>> )
>> globals().update((k, namedvalue(k, v)) for k, v in _named_colours)
>>
>
> A method like Colors.make_global([namespace]**) could be added to do the
> same thing.
>
>
Colors.make_global(__name__)

Michael


>
>
>> (where namedvalue is the value based factory function I mentioned in
>> the recipe post)
>>
>> However, my contention is that the fundamentally interesting operation
>> is associating names with values (as your EnumValue class does). Enums
>> and their ilk are then just syntactic sugar for defining groups of
>> such values without needing to repeat yourself.
>>
>> Cheers,
>> Nick.
>>
>>
> Best Regards,
> Ezio Melotti
>
> ______________________________**_________________
> Python-ideas mailing list
> Python-ideas at python.org
> http://mail.python.org/**mailman/listinfo/python-ideas<http://mail.python.org/mailman/listinfo/python-ideas>
>



-- 

http://www.voidspace.org.uk/

May you do good and not evil
May you find forgiveness for yourself and forgive others
May you share freely, never taking more than you give.
-- the sqlite blessing http://www.sqlite.org/different.html
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20110728/f1a0d90a/attachment.html>


More information about the Python-ideas mailing list