# python中判断变量的类型

python中判断变量的类型

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

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

``` 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() 会认为子类是一种父类类型，考虑继承关系。

posted on
2018-07-19 11:20

) 评论(
)

原文作者：weixin_30477293
原文地址: https://blog.csdn.net/weixin_30477293/article/details/95422676
本文转自网络文章，转载此文章仅为分享知识，如有侵权，请联系博主进行删除。