isinstance is broken

Michael Hudson mwh at python.net
Tue Jan 21 05:04:18 EST 2003


Carl Banks <imbosol at vt.edu> writes:

> Michael Hudson wrote:
> > Carl Banks <imbosol at vt.edu> writes:
> > 
> >> I think a C extension could create an object that is not an instance
> >> of object, in which case isinstance(something,object) would return
> >> false.
> > 
> > I think not, because a type is always an object.
> > 
> > isinstance(c, object) == issubclass(type(c), object) == True.
> >                      ^
> >           except for old-style instances 
> > 
> > My reading of the source would seem to confirm this, but I could well
> > have missed something...
> 
> Correct.  If you leave tp_bases empty, Python happily sets it to a
> tuple of PyBaseObject.

It's even more than that: if your type doesn't have
Py_TPFLAGS_HAVE_CLASS in tp_flags (in which case I don't think
tp_bases is even defined), this code executes in PyType_IsSubtype:

	if (!(a->tp_flags & Py_TPFLAGS_HAVE_CLASS))
		return b == a || b == &PyBaseObject_Type;

(which wasn't the code I found yesterday, but it strengthens my view).

So Everything Is An object.  I'm sure the Smalltalkers are very happy
:)

Cheers,
M.

-- 
  For every complex problem, there is a solution that is simple,
  neat, and wrong.                                    -- H. L. Mencken




More information about the Python-list mailing list