isinstance(object, classinfo)
判斷實(shí)例是否是這個(gè)類(lèi)或者object
object是變量??
classinfo 是類(lèi)型(tuple,dict,int,float)?
判斷變量是否是這個(gè)類(lèi)型??
class objA:??
pass??
?
A = objA()??
B = 'a','v'??
C = 'a string'??
?
print isinstance(A, objA)??
print isinstance(B, tuple)??
print isinstance(C, basestring)??
輸出結(jié)果:
True??
True??
True??
??
不僅如此,還可以利用isinstance函數(shù),來(lái)判斷一個(gè)對(duì)象是否是一個(gè)已知的類(lèi)型。?
isinstance說(shuō)明如下:?
??? 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.).?
其第一個(gè)參數(shù)為對(duì)象,第二個(gè)為類(lèi)型名或類(lèi)型名的一個(gè)列表。其返回值為布爾型。若對(duì)象的類(lèi)型與參數(shù)二的類(lèi)型相同則返回True。若參數(shù)二為一個(gè)元組,則若對(duì)象類(lèi)型與元組中類(lèi)型名之一相同即返回True。?
>>>isinstance(lst, list)?
True?
?
>>>isinstance(lst, (int, str, list) )?
True?
另外:Python可以得到一個(gè)對(duì)象的類(lèi)型 ,利用type函數(shù):>>>lst = [1, 2, 3]>>>type(lst)
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
