版本
E:\Projects\testTool>python --version
Python 3.6.2
定义
先看一下官网如何定义:
issubclass(class, classinfo):
Return true if class is a subclass(direct, indirect or virtual) of classinfo. A class is considerd a subclass of itself. classinfo may be a tuple of class objects, in which case every entry in classinfo will be checked. In any other case, a TypeErroe exception is raised.
大致翻译一下:
如果class参数是classinfo参数的直接或间接或虚拟子类,函数返回True。类是自己本身的子类。classinfo参数可以是一个包含多个类的元组,元组里的所有元素都将被检查一遍。如果class参数不是类,或classinfo元组中没有包含类,函数都会抛出TypeError错误。
总结一下:
issubclass()方法用于判断参数class是否是类型参数classinfo的子类。
示例
1.class参数是classinfo的子类
>>> class A:
... pass
...
>>> class B(A):
... pass
...
>>> class C(A):
... pass
...
>>> issubclass(B, A)
True
>>> issubclass(C, A)
True
>>> issubclass(C, B)
False
2.class参数是classinfo的子类,并且classinfo是元组
返回True:
>>> issubclass(C, (A, object))
True
>>> issubclass(C, (A, int, object))
True
返回Error:
>>> issubclass(C, (int, str))
False
>>> issubclass(C, (int, str, type))
False
抛出TypeError错误:
>>> issubclass(C, (1, A))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: issubclass() arg 2 must be a class or tuple of classes