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()。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM