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

Ethan Furman ethan at stoneleaf.us
Fri Apr 26 01:53:31 CEST 2013


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


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

--
~Ethan~


More information about the Python-Dev mailing list