新聞中心
如果函數(shù)的第一個(gè)參數(shù)是第二個(gè)參數(shù)的實(shí)例或子類(lèi),則isinstance()函數(shù)返回 true。實(shí)際上,我們可以說(shuō)這個(gè)函數(shù)用于檢查給定的對(duì)象是給定類(lèi)的實(shí)例還是子類(lèi)。

在本溪等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強(qiáng)發(fā)展的系統(tǒng)性、市場(chǎng)前瞻性、產(chǎn)品創(chuàng)新能力,以專(zhuān)注、極致的服務(wù)理念,為客戶(hù)提供成都做網(wǎng)站、網(wǎng)站制作 網(wǎng)站設(shè)計(jì)制作按需網(wǎng)站策劃,公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),成都品牌網(wǎng)站建設(shè),網(wǎng)絡(luò)營(yíng)銷(xiāo)推廣,外貿(mào)網(wǎng)站制作,本溪網(wǎng)站建設(shè)費(fèi)用合理。
**isinstance(object, classinfo) ** # Where object specify name of the object
isinstance()參數(shù):
接受 2 個(gè)參數(shù),其中第一個(gè)參數(shù)是 string、int、float、long 或自定義類(lèi)型的對(duì)象。
| 參數(shù) | 描述 | 必需/可選 |
|---|---|---|
| 目標(biāo) | 要檢查的對(duì)象。 | 需要 |
| 分類(lèi)(classify) | 類(lèi)名或類(lèi)名元組。 | 需要 |
isinstance()返回值
它返回真或假的布爾值。
| 投入 | 返回值 | | 對(duì)象是一個(gè)實(shí)例 | 真實(shí)的 | | 對(duì)象不是實(shí)例 | 錯(cuò)誤的 | | classinfo 不是類(lèi)型或類(lèi)型元組 | TypeError exception |
Python 中isinstance()方法的示例
示例isinstance()是如何工作的?
class Foo:
a = 5
fooInstance = Foo()
print(isinstance(fooInstance, Foo))
print(isinstance(fooInstance, (list, tuple)))
print(isinstance(fooInstance, (list, tuple, Foo)))
輸出:
True
False
True
示例 2:使用本機(jī)類(lèi)型處理isinstance()
numbers = [1, 2, 3]
result = isinstance(numbers, list)
print(numbers,'instance of list?', result)
result = isinstance(numbers, dict)
print(numbers,'instance of dict?', result)
result = isinstance(numbers, (dict, list))
print(numbers,'instance of dict or list?', result)
number = 5
result = isinstance(number, list)
print(number,'instance of list?', result)
result = isinstance(number, int)
print(number,'instance of int?', result)
輸出:
[1, 2, 3] instance of list? True
[1, 2, 3] instance of dict? False
[1, 2, 3] instance of dict or list? True
5 instance of list? False
5 instance of int? True
示例isinstance()的工作方式另一個(gè)示例?
# Python `isinstance()` function example
class Student:
id = 101
name = "John"
def __init__(self, id, name):
self.id=id
self.name=name
student = Student(1010,"John")
lst = [12,34,5,6,767]
# Calling function
print(isinstance(student, Student)) # isinstance of Student class
print(isinstance(lst, Student))
輸出:
True
False 新聞名稱(chēng):創(chuàng)新互聯(lián)Python教程:Pythonisinstance()
文章路徑:http://www.5511xx.com/article/dpdiphe.html


咨詢(xún)
建站咨詢(xún)
