Как определить тип экземпляра класса

Для определения класса я могу сделать так:

class A: pass    
a = A

type(A) is type #True

или:

import inspect
inspect.isclass(A) 

А вот как определить тип экземпляра класса, не зная имени класса?

Примерно так:

isinstance(a, a.__class__.__name__)
#TypeError: isinstance() arg 2 must be a type or tuple of types

Я нашел одно решение, но оно не работает с Python 3x

import types

class A: pass
a = A()

print(type(a) == types.InstanceType)
#AttributeError: 'module' object has no attribute 'InstanceType'

Решение:

if '__dict__' in dir(a) and type(a) is not type:
15
задан Opsa 3 March 2012 в 19:44
поделиться