python中isinstance函数如何判断各种类型的小细节-创新互联
创新互联www.cdcxhl.cn八线动态BGP香港云服务器提供商,新人活动买多久送多久,划算不套路!

这篇文章给大家分享的是有关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)
True3. 数字的类型判断
数字分为 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中isinstance函数如何判断各种类型的小细节-创新互联
网址分享:http://www.fbiktj.com/article/ghosj.html
其他资讯
- 公司网址打不开是什么原因,公司网站打不开是什么原因
- 安装mysql后怎么登陆,mysql怎么安装成功
- linux的文件删除命令 linux删除文件命令行
- 交换两个函数的值c语言 交换两个函数的值c语言怎么写
- 电脑开机后间断性供电,风扇一下一下的转,不能正常开机,请问是什么原因?(间隙性访问不稳定主机是昨晚刚刚买的)
- windows三大操作系统:IE,Linux,Windows(ielinux)
- 想爱容易相处难:当ASP.NETMVC爱上IoC
- 境外游戏服务器租用?(境外游戏服务器租用多少钱)
- 微信打开文件打印后会留底吗?(windows8文件播放痕迹)
- 360安全浏览器和极速浏览器的区别?(网站被挂马已经处理了而且也递交申请白名单了)


咨询
建站咨询
