NO IMAGE
isinstance
isinstance(object, classinfo) 
判斷例項是否是這個類或者object是變數
classinfo 是型別(tuple,dict,int,float)
判斷變數是否是這個型別 
class objA: 
pass 
A = objA() 
B = 'a','v' 
C = 'a string' 
print isinstance(A, objA) 
print isinstance(B, tuple) 
print isinstance(C, basestring) 
輸出結果: 
True 
True 
True 
不僅如此,還可以利用isinstance函式,來判斷一個物件是否是一個已知的型別。
isinstance說明如下:
isinstance(object, class-or-type-or-tuple) -> bool
Return whether an object is an instance of a class or of a subclass thereof.
With a type as second argument, return whether that is the object's type.
The form using a tuple, isinstance(x, (A, B, ...)), is a shortcut for
isinstance(x, A) or isinstance(x, B) or ... (etc.).
其第一個引數為物件,第二個為型別名或型別名的一個列表。其返回值為布林型。若物件的型別與引數二的型別相同則返回True。若引數二為一個元組,則若物件型別與元組中型別名之一相同即返回True。
>>>isinstance(lst, list)
True
>>>isinstance(lst, (int, str, list) )
True
另外:Python可以得到一個物件的型別 ,利用type函式:>>>lst = [1, 2, 3]>>>type(lst)<type 'list'>