2.2 features

Guido van Rossum guido at zope.com
Wed Aug 1 01:09:43 CEST 2001


>     >> I guess I'm confused, but if isinstance(x, type) is true isn't
>     >> issubclass(x.__class__, type) also true?
> 
>     Guido> You are indeed confused. :)
> 
>     Guido> Seems you confuse isinstance(x, y) with issubclass(x, y).  These
>     Guido> are very different.  x in y can map to at most one of these (for
>     Guido> y a type object).
> 
> Here's an example that makes concrete what I was thinking:
> 
>     >>> class Foo:
>     ...   pass
>     ... 
>     >>> class Bar(Foo):
>     ...   pass
>     ... 
>     >>> x = Bar()
>     >>> isinstance(x, Foo)
>     1
>     >>> issubclass(Bar, Foo)
>     1
>     >>> issubclass(x.__class__, Foo)
>     1
> 
> Why can't the "in" operator grok all three of these possibilities?
> 
>     x in Foo                         same as isinstance(x, Foo)
>     Bar in Foo                       same as issubclass(Bar, Foo)
>     x.__class__ in Foo               same as issubclass(x.__class__, Foo)
> 
> I assume x can't be both an instance and a class at the same time.

Ah, but there you're wrong.  Classes are also instances (of the
metaclass), and when we don't know anything about an object X, after
knowing that "X in Foo" is true, we would still not know whether X was
a Foo subclass or a Foo instance.  Very different beasts.

This kind of confusion comes up a long when you get too close to
metaclasses (that's why explaining anything about metaclasses is so
difficult).  Languages with a prototype model instead of a
class/instance model don't have this, but they have other issues...

--Guido van Rossum (home page: http://www.python.org/~guido/)





More information about the Python-list mailing list