[Python-ideas] PEP for enum library type?

Greg Ewing greg.ewing at canterbury.ac.nz
Fri Feb 15 06:37:47 CET 2013


João Bernardo wrote:
> And now for something completely different:
> 
> If the Enums are used with attribute syntax, why not this?
> 
>> >> Enum.Colors.red.green.blue
> 
> <Enum: Colors>

Interesting idea, but there seem to be a couple of problems.
It looks like you intend Enum to keep a record of all the
enum types you create with it. What happens if two unrelated
modules both define an enum called Colors?

To avoid this problem, you'd want to assign the newly
created enum type to a local name, but then you need to
write the name twice:

    Colors = Enum.Colors.red.green.blue

or

    Colors = Enum("Colors").red.green.blue

Then what happens if you mistakenly write:

    print(Colors.pineapple)

Seems like this would inadvertently add a new value to the
enum instead of producing an AttributeError.

-- 
Greg



More information about the Python-ideas mailing list