[Python-ideas] PEP for enum library type?
João Bernardo
jbvsmo at gmail.com
Fri Feb 15 07:38:22 CET 2013
This is just an idea to avoid class definition, but it can be improved.
2013/2/15 Greg Ewing <greg.ewing at canterbury.ac.nz>
> 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?
>
>
Enum could write it in the current namespace, just like the `class`
statement.
>>> Enum.Colors.red.green.blue
>>> Colors.red
<Colors.red = 0>
> 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)
>
>
This could be solved with a function call to tell the enum to stop
accepting new values.
>>> Enum.Colors.red.green.blue()
>>> Colors.pineapple
AttributeError: 'Color' object has no attribute 'pineaple'
> Seems like this would inadvertently add a new value to the
> enum instead of producing an AttributeError.
>
> --
> Greg
>
> ______________________________**_________________
> 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>
>
Each attribute access on `Enum` would create a new class even if it's
already defined in the same namespace.
The use of the name `Enum` is just good for definition anyway.
João Bernardo
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20130215/ba7f2a41/attachment.html>
More information about the Python-ideas
mailing list