首页 > 其他 > 详细

type和isinstance

时间:2016-01-14 22:24:43      阅读:305      评论:0      收藏:0      [点我收藏+]
  • type()

判断基本类型:

1 >>> type(123)
2 <type ‘int‘>
3 >>> type(‘str‘)
4 <type ‘str‘>
5 >>> type(None)
6 <type ‘NoneType‘>

 函数或者类:

1 >>> type(abs)
2 <type ‘builtin_function_or_method‘>
3 >>> type(a)
4 <class ‘__main__.Animal‘>

 types模块:

1 >>> import types
2 >>> type(‘abc‘)==types.StringType
3 True
4 >>> type(u‘abc‘)==types.UnicodeType
5 True
6 >>> type([])==types.ListType
7 True
8 >>> type(str)==types.TypeType
9 True

 

  • isinstance()

判断基本类型:

1 >>> isinstance(‘a‘, str)
2 True
3 >>> isinstance(‘a‘, (str, unicode))
4 True
5 >>> isinstance(u‘a‘, (str, unicode))
6 True

 判断继承关系:

1 >>> a = Animal()
2 >>> d = Dog()
3 >>> h = Husky()
4 
5 >>> isinstance(h, Husky)
6 True
7 >>> isinstance(h, Dog)
8 True

 

type和isinstance

原文:http://www.cnblogs.com/utopia8/p/5131745.html

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!