[Python-Dev] Enumeration items: `type(EnumClass.item) is EnumClass` ?

Guido van Rossum guido at python.org
Mon Apr 29 21:04:35 CEST 2013


On Mon, Apr 29, 2013 at 11:34 AM, Larry Hastings <larry at hastings.org> wrote:
> What's the problem with overriding the isinstance checks?  You mention it
> but seem to assume it's a bad idea.  That seems to me like it'd adequately
> solve that problem with an acceptable level of magic.

Depending on whether you are using isinstance() to check if an enum
value belongs in the set of acceptable enums, or to check it it makes
sense to call a certain method, you need isinstance() to behave
differently.

In particular, the default isinstance() would correctly state that
MoreColor.red is not a MoreColor instance, so any methods defined only
on MoreColor should not be called. OTOH it gives the wrong answer if
you are checking that Color.red is an acceptable MoreColor value. But
if you override isinstance() to give the right answer for the latter
(so isinstance(Color.red, MoreColor) is True), then you could
incorrectly conclude that it is safe to call a MoreColor method on
Color.red.

Please do read the StackOverflow links I gave:

http://stackoverflow.com/questions/4604978/subclassing-an-enum
http://stackoverflow.com/questions/3427947/enumeration-inheritence-in-java/3428050#3428050

--
--Guido van Rossum (python.org/~guido)


More information about the Python-Dev mailing list