To determine the class, I can do so:
class A: pass
a = A
type(A) is type #True
or:
import inspect
inspect.isclass(A)
But how to determine the type of class instance, not knowing the class name?
Something like this:
isinstance(a, a.__class__.__name__)
#TypeError: isinstance() arg 2 must be a type or tuple of types
I found one solution, but it does not work with Python 3x
import types
class A: pass
a = A()
print(type(a) == types.InstanceType)
#AttributeError: 'module' object has no attribute 'InstanceType'
Solution:
if '__dict__' in dir(a) and type(a) is not type:
type(a)is the type of the instance, i.e., its class.a.__class__is also a reference to the instance’s class, but you should usetype(a).types.InstanceTypeis only for old-style classes in versions of Python pre-3.0, where all instances had the same type. You should be using new-style classes (derived fromobject) in 2.x. In Python 3.0, all classes are new-style classes.