[Python-Dev] Semantic of isinstance

Phillip J. Eby pje at telecommunity.com
Tue Jun 27 15:53:07 CEST 2006


At 09:29 AM 6/27/2006 +0200, Maric Michaud wrote:
>Le mardi 27 juin 2006 05:38, Phillip J. Eby a écrit :
> > As it happens, this is due to the fact that E is a type, while E() is
> > not.  There's an optimization in the isinstance() machinery that simply
> > checks to see if D().__class__ is a subtype of E.  That's where your
> > experiment fails.
> >
> > I'm not sure whether this behavior should be considered correct or not.
> >
>
>Doesn't seems to be just related to isinstance implementation,

That may be, but it's due to code that's entirely 
independent.  isinstance() does what it does independently of the behavior 
you describe below, which is a product of how descriptors behave in new and 
old-style classes.  The behavior you describe below is a natural 
consequence of the documented behavior of descriptors, while the above 
behavior is a performance optimization in isinstance().  They aren't related.


>furthermore, it
>is very surprising that old-style and new style classes behave exactly the
>opposite way.
>
>In [2]: class a(object) :
>    ...:     __class__ = 0
>    ...:
>    ...:
>
>In [3]: a.__class__
>Out[3]: <type 'type'>
>
>In [4]: a().__class__
>Out[4]: 0
>
>In [7]: class a :
>    ...:     __class__ = 0
>    ...:
>    ...:
>
>In [8]: a.__class__
>Out[8]: 0
>
>In [9]: a().__class__
>Out[9]: <class __main__.a at 0xa78cb4ac>



More information about the Python-Dev mailing list