python中判断变量的类型

ゝ一纸荒年。 2022-04-11 07:57 340阅读 0赞

原文:https://www.cnblogs.com/xmnote/p/9334743.html

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

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

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

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

参数1:变量

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

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

例子:

复制代码

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

复制代码

返回:

1118859-20180719111155592-1613687455.png

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

例子:

复制代码

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

复制代码

返回:

1118859-20180719111155592-1613687455.png

补充: 

isinstance() 与 type() 区别:

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

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

发表评论

表情:
评论列表 (有 0 条评论,340人围观)

还没有评论,来说两句吧...

相关阅读