python中判断变量的类型

python的数据类型有:数字(int)、浮点(float)、字符串(str),列表(list)、元组(tuple)、字典(dict)、集合(set)

一般通过以下方法进行判断:

1、isinstance(参数1,参数2)

描述:该函数用来判断一个变量(参数1)是否是已知的变量类型(参数2) 类似于type()

参数1:变量

参数2:可以是直接或间接类名、基本类型或者由它们组成的元组。

返回值: 如果对象的类型与参数二的类型(classinfo)相同则返回 True,否则返回 False

例子:

复制代码
 1 #判断变量类型的函数
 2 def typeof(variate):
 3     type=None
 4     if isinstance(variate,int):
 5         type = "int"
 6     elif isinstance(variate,str):
 7         type = "str"
 8     elif isinstance(variate,float):
 9         type = "float"
10     elif isinstance(variate,list):
11         type = "list"
12     elif isinstance(variate,tuple):
13         type = "tuple"
14     elif isinstance(variate,dict):
15         type = "dict"
16     elif isinstance(variate,set):
17         type = "set"
18     return type
19 # 返回变量类型
20 def getType(variate):
21     arr = {"int":"整数","float":"浮点","str":"字符串","list":"列表","tuple":"元组","dict":"字典","set":"集合"}
22     vartype = typeof(variate)
23     if not (vartype in arr):
24         return "未知类型"
25     return arr[vartype]
26     
27 #判断变量是否为整数
28 money=120
29 print("{0}是{1}".format(money,getType(money)))
30 #判断变量是否为字符串
31 money="120"
32 print("{0}是{1}".format(money,getType(money)))
33 money=12.3
34 print("{0}是{1}".format(money,getType(money)))
35 #判断变量是否为列表
36 students=['studentA']
37 print("{0}是{1}".format(students,getType(students)))
38 #判断变量是否为元组
39 students=('studentA','studentB')
40 print("{0}是{1}".format(students,getType(students)))
41 #判断变量是否为字典
42 dictory={"key1":"value1","key2":"value2"}
43 print("{0}是{1}".format(dictory,getType(dictory)))
44 #判断变量是否为集合
45 apple={"apple1","apple2"}
46 print("{0}是{1}".format(apple,getType(apple)))
复制代码

返回:

 

2、通过与已知类型的常量进行比较

例子:

复制代码
 1 #判断变量类型的函数
 2 def typeof(variate):
 3     type1 = ""
 4     if type(variate) == type(1):
 5         type1 = "int"
 6     elif type(variate) == type("str"):
 7         type1 = "str"
 8     elif type(variate) == type(12.3):
 9         type1 = "float"
10     elif type(variate) == type([1]):
11         type1 = "list"
12     elif type(variate) == type(()):
13         type1 = "tuple"
14     elif type(variate) == type({"key1":"123"}):
15         type1 = "dict"
16     elif type(variate) == type({"key1"}):
17         type1 = "set"
18     return type1
19 # 返回变量类型
20 def getType(variate):
21     arr = {"int":"整数","float":"浮点","str":"字符串","list":"列表","tuple":"元组","dict":"字典","set":"集合"}
22     vartype = typeof(variate)
23     if not (vartype in arr):
24         return "未知类型"
25     return arr[vartype]
26     
27 #判断变量是否为整数
28 money=120
29 print("{0}是{1}".format(money,getType(money)))
30 #判断变量是否为字符串
31 money="120"
32 print("{0}是{1}".format(money,getType(money)))
33 money=12.3
34 print("{0}是{1}".format(money,getType(money)))
35 #判断变量是否为列表
36 students=['studentA']
37 print("{0}是{1}".format(students,getType(students)))
38 #判断变量是否为元组
39 students=('studentA','studentB')
40 print("{0}是{1}".format(students,getType(students)))
41 #判断变量是否为字典
42 dictory={"key1":"value1","key2":"value2"}
43 print("{0}是{1}".format(dictory,getType(dictory)))
44 #判断变量是否为集合
45 apple={"apple1","apple2"}
46 print("{0}是{1}".format(apple,getType(apple)))
复制代码

返回:

 补充: 

isinstance() 与 type() 区别:

    • type() 不会认为子类是一种父类类型,不考虑继承关系。

    • isinstance() 会认为子类是一种父类类型,考虑继承关系。

如果要判断两个类型是否相同推荐使用 isinstance()。

posted @ 2021-04-01 10:10  张岂逢  阅读(300)  评论(0编辑  收藏  举报