关于isinstance使用(节选)

来源:互联网 发布:网络课程平台 编辑:程序博客网 时间:2024/04/27 14:08

type() 函数提供对象的类型时,还可以使用 isinstance() 函数测试对象,以确定它是否是某个特定类型或定制类的实例:

>>> print isinstance.__doc__isinstance(object, class-or-type-or-tuple) -> BooleanReturn whether an object is an instance of a class or of a subclass thereof.With a type as second argument, return whether that is the object's type.The form using a tuple, isinstance(x, (A, B, ...)), is a shortcut forisinstance(x, A) or isinstance(x, B) or ... (etc.).>>> isinstance(42, str)0>>> isinstance('a string', int)0>>> isinstance(42, int)1>>> isinstance('a string', str)1