[Python-Dev] PEP 435 -- Adding an Enum type to the Python standard library

Glenn Linderman v+python at g.nevcal.com
Fri Apr 26 04:09:14 CEST 2013

On 4/25/2013 4:53 PM, Ethan Furman wrote:
> On 04/25/2013 04:26 PM, Glenn Linderman wrote:
>> My question is, once an enumeration is defined, is there a way, short 
>> of element-by-element assignment, to import the
>> individual enumeration instances into the current namespace, so that 
>> I can say "red" instead of "Color.red" ? I
>> understand the benefits of avoiding name collisions when there are 
>> lots of enumerations, and lots of opportunities for
>> name collections between, say, RGBColor and CYMKColor... but there 
>> are lots of uses for enumerations where the
>> subsidiary namespace is just aggravating noise.
> You mean something like:
> --> class Color(Enum):
> ...     RED = 1
> ...     GREEN = 2
> ...     BLUE = 3
> --> Color.register()  # puts Color in sys.modules
> --> from Color import *  # doesn't work in a function, though :(
> --> BLUE
> Color.BLUE

Something like that, but that works in a function too :)

> Yeah, that would be nice.  ;)  A bit dangerous, though -- what if 
> another module does the same thing, but its Color is different?
> Better would be:
> --> Color.export(globals())  # put the enumerators in globals
> --> RED
> Color.RED

Globals? locals should be possible too.

Or even something like:

with Color:

Although the extra indentation could also be annoying.

One wouldn't want the module defining Color to automatically 'export' 
the colors: but rather a way to request an 'export' them into a 
particular scope. That way the proliferation of names into scopes is 
chosen by the programmer.

import module_containing_color
module_containing_color.Color.export_enumerations( globals )


import module_containing_color
module_containing_color.Color.export_enumerations( locals )

Or maybe locals is implicit, and in the file scope of a module, locals 
are globals anyway, so doing


would make the enumerations available to all definitions in the file, 
but inside a class or def doing the same thing would make the names 
direct members of the class or locals in the function.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20130425/18a5b562/attachment.html>

More information about the Python-Dev mailing list