首页 > 编程 > Python > 正文

Python 判断变量的数据类型

2019-11-08 02:54:31
字体:
来源:转载
供稿:网友
import types aaa = 0 PRint type(aaa) if type(aaa) is types.IntType: print "the type of aaa is int" if isinstance(aaa,int): print "the type of aaa is int" bbb = 'hello' print type(bbb) if type(bbb) is types.StringType: print "the type of bbb is string" if isinstance(bbb,str): print "the type of bbb is string" #if the type is NoneType,the isinstance does not work #we should judge the NoneType like below #if row is None #if type(row) is types.NoneType #In my opinion,use the types to judge the type of a param is convinient, use the isinstance to judge whether a instance is a type of a class or not
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表