python中判斷變量的類型

python的數據類型有:數字(int)、浮點(float)、字符串(str),列表(list)、元組(tuple)、字典(dict)、集合(set)

一般通過以下方法進行判斷:

1、isinstance(參數1,參數2)

描述:該函數用來判斷一個變量(參數1)是否是已知的變量類型(參數2) 類似於type()

參數1:變量

參數2:可以是直接或間接類名、基本類型或者由它們組成的元組。

返回值: 如果對象的類型與參數二的類型(classinfo)相同則返回 True,否則返回 False

例子:

#判斷變量類型的函數
def typeof(variate):
    type=None
    if isinstance(variate,int):
        type = "int"
    elif isinstance(variate,str):
        type = "str"
    elif isinstance(variate,float):
        type = "float"
    elif isinstance(variate,list):
        type = "list"
    elif isinstance(variate,tuple):
        type = "tuple"
    elif isinstance(variate,dict):
        type = "dict"
    elif isinstance(variate,set):
        type = "set"
    return type
# 返回變量類型
def getType(variate):
    arr = {"int":"整數","float":"浮點","str":"字符串","list":"列表","tuple":"元組","dict":"字典","set":"集合"}
    vartype = typeof(variate)
    if not (vartype in arr):
        return "未知類型"
    return arr[vartype]
    
#判斷變量是否爲整數
money=120
print("{0}是{1}".format(money,getType(money)))
#判斷變量是否爲字符串
money="120"
print("{0}是{1}".format(money,getType(money)))
money=12.3
print("{0}是{1}".format(money,getType(money)))
#判斷變量是否爲列表
students=['studentA']
print("{0}是{1}".format(students,getType(students)))
#判斷變量是否爲元組
students=('studentA','studentB')
print("{0}是{1}".format(students,getType(students)))
#判斷變量是否爲字典
dictory={"key1":"value1","key2":"value2"}
print("{0}是{1}".format(dictory,getType(dictory)))
#判斷變量是否爲集合
apple={"apple1","apple2"}
print("{0}是{1}".format(apple,getType(apple)))

返回:

2、通過與已知類型的常量進行比較

例子:

#判斷變量類型的函數
def typeof(variate):
    type1 = ""
    if type(variate) == type(1):
        type1 = "int"
    elif type(variate) == type("str"):
        type1 = "str"
    elif type(variate) == type(12.3):
        type1 = "float"
    elif type(variate) == type([1]):
        type1 = "list"
    elif type(variate) == type(()):
        type1 = "tuple"
    elif type(variate) == type({"key1":"123"}):
        type1 = "dict"
    elif type(variate) == type({"key1"}):
        type1 = "set"
    return type1
# 返回變量類型
def getType(variate):
    arr = {"int":"整數","float":"浮點","str":"字符串","list":"列表","tuple":"元組","dict":"字典","set":"集合"}
    vartype = typeof(variate)
    if not (vartype in arr):
        return "未知類型"
    return arr[vartype]
    
#判斷變量是否爲整數
money=120
print("{0}是{1}".format(money,getType(money)))
#判斷變量是否爲字符串
money="120"
print("{0}是{1}".format(money,getType(money)))
money=12.3
print("{0}是{1}".format(money,getType(money)))
#判斷變量是否爲列表
students=['studentA']
print("{0}是{1}".format(students,getType(students)))
#判斷變量是否爲元組
students=('studentA','studentB')
print("{0}是{1}".format(students,getType(students)))
#判斷變量是否爲字典
dictory={"key1":"value1","key2":"value2"}
print("{0}是{1}".format(dictory,getType(dictory)))
#判斷變量是否爲集合
apple={"apple1","apple2"}
print("{0}是{1}".format(apple,getType(apple)))

 

返回:

 補充: 

isinstance() 與 type() 區別:

    • type() 不會認爲子類是一種父類類型,不考慮繼承關係。

    • isinstance() 會認爲子類是一種父類類型,考慮繼承關係。

如果要判斷兩個類型是否相同推薦使用 isinstance()。

發佈了49 篇原創文章 · 獲贊 13 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章