[Tutor] FW: isinstance -> instance

Kent Johnson kent37 at tds.net
Mon Feb 26 23:26:34 CET 2007

Bernard Lebel wrote:
> That's fine, but that tells me that 'a' is an instance of 'A'.
> It doesn't, however, tell me if 'a' is an instance or the actual class object.

I'm not sure what you are trying to do.

In [1]: class A: pass
In [2]: a=A()
In [3]: isinstance(a, A)
Out[3]: True
In [4]: isinstance(A, A)
Out[4]: False

The actual class object is not an instance of itself.

 >>>> So I'm a bit at a loss here. I don't want to know if the instanceis
 >>>> an instance of a specific class, I just want to know if it's an
 >>>> instance.

*Everything* in Python is an instance of *some* class. int, str, classes 
themselves, modules, etc. Everything is an instance of something.

The specific <type 'instance'> is the type of instances of old-style 
classes. There is some funny business going on that I don't fully 
understand but AFAIK instances of old-style classes are in some sense 
all instances of this <type 'instance'>.

If you are trying to test whether an object is an instance of an 
old-style class you can do it like this (though I can't imagine why you 
would want to):

In [10]: class C:
    ....:      pass
In [11]: c=C()
In [12]: isinstance(c, type(a))
Out[12]: True

Or you can import types and check against types.InstanceType:

In [13]: import types
In [14]: dir(types)
In [15]: isinstance(c, types.InstanceType)
Out[15]: True


More information about the Tutor mailing list