On 4/29/2013 10:01 AM, Guido van Rossum wrote:
On Mon, Apr 29, 2013 at 9:12 AM, Ethan Furman <et...@stoneleaf.us> wrote:
On 04/29/2013 08:39 AM, Guido van Rossum wrote:
Indeed, the "type(Color.red) is Color" claim was meant for the
situation where red is defined directly in Color, and I used type()
instead of isinstance() because Barry was proposing to overload
isinstance() to make this true without equating the classes. But for
the subclass case, I want MoreColor.red is Color.red and
isinstance(MoreColor.red, Color), but not isinstance(Color.red,
MoreColor). If you can't live with that, don't subclass enums.
So if I understand:
--> class Color(Enum):
... red = 1
... green = 2
... blue = 3
--> class MoreColor(Color):
... cyan = 4
... magenta = 5
... yellow = 6
--> type(MoreColor.red) is Color
--> type(MoreColor.red) is not MoreColor
In other words, while `red` is accessible in MoreColor, it's actually a
Color instance?
Oh dear, this is actually a mess. I don't want MoreColor.red and
Color.red to be distinct objects, but then the isinstance() checks
will become confusing. If we don't override isinstance(), we'll get
not isinstance(Color.red, MoreColor)
isinstance(MoreColor.yellow, Color)
This would be pretty backwards.
I Ggoogled "enum subclassing" and found this StackOverflow article
explaining why you can't subclass enums in Java:
http://stackoverflow.com/questions/4604978/subclassing-an-enum which
refers to this more elaborate answer:
http://stackoverflow.com/questions/3427947/enumeration-inheritence-in-java/3428050#3428050
I think this conclusively shows that it's better to disallow
subclassing enums. (It also brings enum in line with bool, which is
also a "final" class.)
Adding Eli to the thread explicitly, because this needs to be
explained in the PEP.
The only thing that needs to be disallowed is defining additional named
elements, for Liskov Substitution to not be violated.
So in an inheritance tree derived from Enum, it would be a requirement
that all members of the enumeration be enumerated in one class.
It would be extremely nice if:
1) Enum could be subclassed to provide different, sharable, types of
behaviors, then further subclassed to provide a number of distinct sets
of values with those behaviors.
2) Enum could be subclassed to provide one set of values, and then
further subclassed to provide a number a distinct sets of behaviors for
those sets of values.
(I see a lot more benefit to #1, than #2, if a choice must be made.)
_______________________________________________
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe:
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com