[Python-Dev] PEP-435 reference implementation

Glenn Linderman v+python at g.nevcal.com
Wed May 1 09:09:49 CEST 2013


On 4/30/2013 9:19 PM, Ethan Furman wrote:
> Latest code available at https://bitbucket.org/stoneleaf/aenum.
>
> --> class Color(Enum):
> ...     red = 1
> ...     green = 2
> ...     blue = 3
>
> Enum items are virtual attributes looked by EnumType's __getattr__.  
> The win here is that
>
> --> Color.red.green.blue
>
> no longer works.  ;)

Color.red.green.blue not working seems like a win.

Still seems like it should be possible to look them up from a subclass, 
though.

--> class FancyColor( Color ):
...        'strikes my fancy'

--> FancyColor['red']
Color.red

>
> Subclassing an implemented Enum class now raises an error (is there a 
> better word than 'implemented'?)
>
> --> class MoreColor(Color):
> ...     cyan = 4
> ...     magenta = 5
> ...     yellow = 6
>
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
>   File "./ref435.py", line 83, in __new__
>     raise EnumError("cannot subclass an implemented Enum class")
> ref435.EnumError: cannot subclass an implemented Enum class

Yes, I think raising an error is appropriate, if implementing Guido's 
"no subclass" comments, rather than treating what otherwise would look 
like enumeration settings as subclass attributes.

My suggested error wording would be "Cannot define additional 
enumeration items in a subclass".

That allows for "original enumeration items" to be defined in a 
subclass, of course.  And it isn't the subclassing that is disallowed, 
but the definition of more enumeration items that is disallowed.  At 
least, I hope that is the case.

Then, should consenting adults lift the restriction, there wouldn't be 
surprises in other code.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20130501/4f21397b/attachment-0001.html>


More information about the Python-Dev mailing list