isinstance的参数
isinstance的参数是不是只能是str?还是分不同情况
isinstance的参数是不是只能是str?还是分不同情况
2016-07-07
isinstance函数的原型是:
isinstance(object, classinfo)
Return true if the object argument is an instance of the classinfo argument, or of a (direct or indirect) subclass thereof. Also return true if classinfo is a type object (new-style class) and object is an object of that type or of a (direct or indirect) subclass thereof. If object is not a class instance or an object of the given type, the function always returns false. If classinfo is neither a class object nor a type object, it may be a tuple of class or type objects, or may recursively contain other such tuples (other sequence types are not accepted). If classinfo is not a class, type, or tuple of classes, types, and such tuples, a TypeError exception is raised.
Changed in version 2.2: Support for a tuple of type information was added.
其中的classinfo指的是某种类型,比如常用的unicode:
isinstance(some_str, unicode)
来判断是否是unicode类型的字符。但是想要知道其他还有哪些类型,网上没找到具体说明,后来还是自己在python安装后,自带的的英文manual中找到了:
5.4. Numeric Types — int, float, long, complex
5.6. Sequence Types — str, unicode, list, tuple, bytearray, buffer, xrange
5.7. Set Types — set, frozenset
5.8. Mapping Types — dict
这样,才知道,关于想要判断某个变量的类型,就可以去手册里面找到对应的变量名,然后测试是否是该类型变量了。
举报