Jquery中文网 www.jquerycn.cn
Jquery中文网 >  Python编程  >  Python入门  >  正文 python中isinstance函数判断各种类型的小细节

python中isinstance函数判断各种类型的小细节

发布时间:2021-05-24   编辑:www.jquerycn.cn
jquery中文网为您提供python中isinstance函数判断各种类型的小细节等资源,欢迎您收藏本站,我们将为您提供最新的python中isinstance函数判断各种类型的小细节资源

1. 基本语法

isinstance(object, classinfo)

Return true if the object argument is an instance of the classinfo argument, or of a (direct, indirect or virtual) 
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, indirect or virtual) 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.

classinfo 处可以是 a class, type, or tuple of classes and types,

如果是 tuple,则满足 tuple 中的任何一个即返回 True

2. 字符串的类型判断。

字符串,分为 str 和 unicode,二者均继承自 basestring

>>> isinstance(u'3.0', unicode)
True
>>> isinstance('3.0', str)
True
>>> isinstance(u'3.0', str)
False
>>> isinstance(u'3.0', str)
False
>>> isinstance(u'3.0', basestring)
True
>>> isinstance('3.0', basestring)
True

3. 数字的类型判断

数字分为 int 和 float,暂未发现二者共同的有效父类。

可以用 (int, float) tuple 来判断是否为数字(int 或 float)

>>> isinstance('3', (int, float))
False
>>> isinstance(3.0, (int, float))
True
>>> isinstance(3, (int, float))
True
>>> isinstance(3.0, float)
True
>>> isinstance(3.0, int)
False
>>> isinstance(3, float)
False
>>> isinstance(3, int)
True

您可能感兴趣的文章:
python中isinstance函数判断各种类型的小细节
python如何判断数据类型
python怎么区分不同数据类型
python里怎么查看数据类型
如何利用Python中isinstance()判断变量类型
python判断变量的类型吗
python怎么判断整数
python怎么判断数据类型
python 判断变量是数字型还是字符型
python如何判断变量是否是字符串

[关闭]