isinstance(object,classinfo)
- 返回True,如果object是classinfo或者classinfo子class的实例。
- 如果classinfo是包含type和class的tuple,只要满足其中一个的实例,就返回True。
- object不是实例,返回False。
- classinfo不是class或者type,触发TypeError。
>>> isinstance(2,int)
True
>>> isinstance(2,(int,float))
True
>>> isinstance(2,[int,float])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: isinstance() arg 2 must be a class, type, or tuple of classes and types
issubclass(class,classinfo)
>>> class a:
... pass
>>> class b(a):
... pass
>>> class c(a):
... pass
...
>>> issubclass(a,a)
True
>>> issubclass(a,b)
False
>>> issubclass(b,a)
True
>>> issubclass(b,[a,c])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: issubclass() arg 2 must be a class or tuple of classes
>>> issubclass(b,(a,c))
True
>>> issubclass(b,(c,a))
True