日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

Python内置函数(49)——isinstance

發布時間:2025/3/15 python 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Python内置函数(49)——isinstance 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

英文文檔:

isinstance(object,?classinfo)

Return true if the?object?argument is an instance of the?classinfo?argument, or of a (direct, indirect or?virtual) subclass thereof. If?object?is not an object of the given type, the function always returns false. If?classinfo?is a tuple of type objects (or recursively, other such tuples), return true if?object?is an instance of any of the types. If?classinfo?is not a type or tuple of types and such tuples, a?TypeError?exception is raised.

?   判斷對象是否是類或者類型元組中任意類元素的實例

說明:

o  1. 函數功能用于判斷對象是否是類型對象的實例,object參數表示需要檢查的對象,calssinfo參數表示類型對象。

  2. 如果object參數是classinfo類型對象(或者classinfo類對象的直接、間接、虛擬子類)的實例,返回True。

>>> isinstance(1,int) True >>> isinstance(1,str) False# 定義3各類:C繼承B,B繼承A >>> class A:pass>>> class B(A):pass>>> class C(B):pass>>> a = A() >>> b = B() >>> c = C() >>> isinstance(a,A) #直接實例 True >>> isinstance(a,B) False >>> isinstance(b,A) #子類實例 True >>> isinstance(c,A) #孫子類實例 True

  3. 如果object參數傳入的是類型對象,則始終返回False。

>>> isinstance(str,str) False >>> isinstance(bool,int) False

  4. 如果classinfo類型對象,是多個類型對象組成的元組,如果object對象是元組的任一類型對象中實例,則返回True,否則返回False。

>>> isinstance(a,(B,C)) False >>> isinstance(a,(A,B,C)) True

  5. 如果classinfo類型對象,不是一個類型對象或者由多個類型對象組成的元組,則會報錯(TypeError)。

>>> isinstance(a,[A,B,C]) Traceback (most recent call last):File "<pyshell#23>", line 1, in <module>isinstance(a,[A,B,C]) TypeError: isinstance() arg 2 must be a type or tuple of types

轉載于:https://www.cnblogs.com/lincappu/p/8145108.html

總結

以上是生活随笔為你收集整理的Python内置函数(49)——isinstance的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。