isinstance函数

  1. isinstance  
  2.   
  3. isinstance(object, classinfo)   
  4. 判断实例是否是这个类或者object是变量  
  5.   
  6. classinfo 是类型(tuple,dict,int,float,bool,complex,str,list,set)
  7. 判断变量是否是这个类型   
  8.   
  9. class objA:   
  10. pass   
  11.   
  12. A = objA()   
  13. B = 'a','v'   
  14. C = 'a string'   
  15.   
  16. print isinstance(A, objA)   
  17. print isinstance(B, tuple)   
  18. print isinstance(C, basestring)   
  19. 输出结果:   
  20. True   
  21. True   
  22. True   
  23.   
  24.    
  25. 不仅如此,还可以利用isinstance函数,来判断一个对象是否是一个已知的类型。  
  26. isinstance说明如下:  
  27.     isinstance(object, class-or-type-or-tuple) -> bool  
  28.       
  29.     Return whether an object is an instance of a class or of a subclass thereof.  
  30.     With a type as second argument, return whether that is the object's type.  
  31.     The form using a tuple, isinstance(x, (A, B, ...)), is a shortcut for  
  32.     isinstance(x, A) or isinstance(x, B) or ... (etc.).  
  33.   
  34. 其第一个参数为对象,第二个为类型名或类型名的一个列表。其返回值为布尔型。若对象的类型与参数二的类型相同则返回True。若参数二为一个元组,则若对象类型与元组中类型名之一相同即返回True。  
  35.   
  36. >>>isinstance(lst, list)  
  37. True  
  38.   
  39. >>>isinstance(lst, (int, str, list) )  
  40. True  
[python] view plaincopy
  1. 另外:Python可以得到一个对象的类型 ,利用type函数:>>>lst = [123]>>>type(lst)<type 'list'>  
posted @   aaronthon  阅读(1536)  评论(0编辑  收藏  举报
编辑推荐:
· .NET制作智能桌面机器人:结合BotSharp智能体框架开发语音交互
· 软件产品开发中常见的10个问题及处理方法
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
· 从问题排查到源码分析:ActiveMQ消费端频繁日志刷屏的秘密
· 一次Java后端服务间歇性响应慢的问题排查记录
阅读排行:
· 互联网不景气了那就玩玩嵌入式吧,用纯.NET开发并制作一个智能桌面机器人(四):结合BotSharp
· 《HelloGitHub》第 108 期
· Windows桌面应用自动更新解决方案SharpUpdater5发布
· 我的家庭实验室服务器集群硬件清单
· Supergateway:MCP服务器的远程调试与集成工具
点击右上角即可分享
微信分享提示