的类型来决定的课,我可以这样做:如何确定类实例
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 3倍的工作
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:
你问如何确定一个对象是一个类还是一个类的实例? – grifaton 2012-03-03 19:06:33
InstanceType是旧样式的类,它在3.x中不存在。 – 2012-03-03 19:07:45
@grifaton,我想区分类和实例 – Opsa 2012-03-03 19:12:24