您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

python isinstance 判断各种类型的简单示例

5b51 2022/1/14 8:14:51 python 字数 3553 阅读 284 来源 www.jb51.cc/python

python isinstance 判断各种类型的简单示例

概述

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,or tuple of classes and types,

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

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


# @param python isinstance 判断各种类型的小细节
# @author 编程之家 jb51.cc|www.www.jb51.cc 

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

# End www.jb51.cc

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

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


# @param python isinstance 判断各种类型的小细节
# @author 编程之家 jb51.cc|www.www.jb51.cc 

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

# End www.jb51.cc

总结

以上是编程之家为你收集整理的python isinstance 判断各种类型的简单示例全部内容,希望文章能够帮你解决python isinstance 判断各种类型的简单示例所遇到的程序开发问题。


如果您也喜欢它,动动您的小指点个赞吧

除非注明,文章均由 laddyq.com 整理发布,欢迎转载。

转载请注明:
链接:http://laddyq.com
来源:laddyq.com
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


联系我
置顶