[Python-ideas] PEP for enum library type?
Joao S. O. Bueno
jsbueno at python.org.br
Tue Feb 12 21:33:13 CET 2013
On 12 February 2013 17:50, Barry Warsaw <barry at python.org> wrote:
>>That is too much ' ' typing - I think it would be ok, to have it like that,
>>but a helper function that would work just like the namedtuple call: Color =
>>Enum("Color", "RED GREEN BLUE", int)
>
> How about:
>
> >>> from flufl.enum import make
> >>> make('Colors', 'RED GREEN BLUE'.split())
> <Colors {RED: 1, GREEN: 2, BLUE: 3}>
Not bad - I think that wahtever is agreeded in a nice api that takes
advantage of the
"class" statement we should have a callabale constructor that takes
names as strings.
And moreover, it is nice to have a way to load the created
enums/constants to the current namespace -
and it can be done, without "magic" to the global namespace having a
call that takes a "namespace" argument
to which one would normally pass in blobals()
So:
from enum import load, make
would allow for:
>>> Colors = make("Colors", "RED GREEN BLUE".split())
>>> load(Colors, globals())
>>> RED
RED
(I think that allowing a call without the "split()" would be better - )
That would make some *linters complain - but those should update
themselves to the fact in time.
I consider a way to load the created constants into the module namespace
very important to the concept of enums/constant itself. Agreeing on a call
that can get "globals()" and update it would preclude the idea of being able
to do "from MyEnumClass import *" that showed up on the previous thread.
js
-><-
More information about the Python-ideas
mailing list