[Tutor] FW: isinstance -> instance
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 : class A: pass
In : a=A()
In : isinstance(a, A)
In : isinstance(A, A)
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
*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 : class C:
In : c=C()
In : isinstance(c, type(a))
Or you can import types and check against types.InstanceType:
In : import types
In : dir(types)
In : isinstance(c, types.InstanceType)
More information about the Tutor