<p dir="ltr"><br>
On 2 May 2013 02:46, "Guido van Rossum" <<a href="mailto:guido@python.org">guido@python.org</a>> wrote:<br>
><br>
> On Wed, May 1, 2013 at 9:18 AM, Tres Seaver <<a href="mailto:tseaver@palladion.com">tseaver@palladion.com</a>> wrote:<br>
> > I'd be glad to drop both of those in favor of subclassing:  I think the<br>
> > emphasis on "class-ness" makes no sense, given the driving usecases for<br>
> > adopting enums into the stdlib in the first place.   IOW, I would vote<br>
> > that real-world usecases trump hypothetical purity.<br>
><br>
> Yeah, this is the dilemma. But what *are* the real-world use cases?<br>
> Please provide some.<br>
><br>
> Here's how I would implement "extending" an enum if subclassing were<br>
> not allowed:<br>
><br>
> class Color(Enum):<br>
>   red = 1<br>
>   white = 2<br>
>   blue = 3<br>
><br>
> class ExtraColor(Enum):<br>
>   orange = 4<br>
>   yellow = 5<br>
>   green = 6<br>
><br>
> flag_colors = set(Color) | set(ExtraColor)<br>
><br>
> Now I can test "c in flag_colors" to check whether c is a flag color.<br>
> I can also loop over flag_colors. If I want the colors in definition<br>
> order I could use a list instead:<br>
><br>
> ordered_flag_colors = list(Color) + list(ExtraColor)<br>
><br>
> But this would be less or more acceptable depending on whether it is a<br>
> common or esoteric use case.</p>
<p dir="ltr">If enums had an "as_dict" method that returned an ordered dictionary, you could do:</p>
<p dir="ltr">class MoreColors(Enum):<br>
    locals().update(Colors.as_dict())<br>
    orange = 4<br>
    ...</p>
<p dir="ltr">Using a similar API to PEP 422's class initialisation hook, you could even simplify that to:</p>
<p dir="ltr">class MoreColors(Enum,  namespace=Colors.as_dict()):<br>
    orange = 4<br>
    ...</p>
<p dir="ltr">Cheers,<br>
Nick.</p>
<p dir="ltr">><br>
> --<br>
> --Guido van Rossum (<a href="http://python.org/~guido">python.org/~guido</a>)<br>
> _______________________________________________<br>
> Python-Dev mailing list<br>
> <a href="mailto:Python-Dev@python.org">Python-Dev@python.org</a><br>
> <a href="http://mail.python.org/mailman/listinfo/python-dev">http://mail.python.org/mailman/listinfo/python-dev</a><br>
> Unsubscribe: <a href="http://mail.python.org/mailman/options/python-dev/ncoghlan%40gmail.com">http://mail.python.org/mailman/options/python-dev/ncoghlan%40gmail.com</a><br>
</p>