1. 程式人生 > >python獲取變數的資料型別

python獲取變數的資料型別

有時候我們需要知道variable的資料型別,在python中有內建函式type可以獲取variable的資料型別

#-*- coding=utf-8 -*-

id1 = 1
typestr = type(id1)
print typestr
print type(typestr)
print typestr.__name__

if "int" == typestr.__name__:
    print True
else:
    print False

結果:

<type 'int'>
<type 'type'>
int
True