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
Python 判断变量的数据类型
最新推荐文章于 2023-09-10 01:53:36 发布
本文介绍如何在Python中使用type()和isinstance()进行变量类型的检查。通过实例演示了整型、字符串类型的判断,并讨论了NoneType类型判断的特殊情况。
1898

被折叠的 条评论
为什么被折叠?



