isinstance is broken
Carl Banks
imbosol at vt.edu
Sat Jan 18 14:39:51 EST 2003
Michele Simionato wrote:
> There are inconsistencies in "isinstance".
>
> Example 1:
>
>>>> isinstance(int,object)
> 1
>
> gives no error message. I don't like it, an error should be
> raised. There is issubclass for that job:
Even if it were wrong--why should it raise an error? If int were not
an instance of object, then it should just return 0 (or False).
>>>> issubclass(int,object)
> 1
int is both a subclass and an instance of object.
For A to be an instance of B, then B has to be either type(A), or any
base class of type(A).
int's type is type.
object is a base class of type.
Therefore, int is an instance of object.
It's called inheritance. It's just like C++ and other typical OO
languages: if an object is an instance of a class, it is also an
instance of that class's bases.
> Should I interpret the fact that isinstance(something,object) always returns
> true as a bug or is it intentional ?
Neither. It's the right behavior. (I don't think it's manifestly
true, BTW. 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. Someone might want to verify.)
--
CARL BANKS
More information about the Python-list
mailing list