新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
創(chuàng)新互聯(lián)Python教程:Pythonisinstance()
如果函數(shù)的第一個參數(shù)是第二個參數(shù)的實例或子類,則isinstance()函數(shù)返回 true。實際上,我們可以說這個函數(shù)用于檢查給定的對象是給定類的實例還是子類。

在本溪等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強發(fā)展的系統(tǒng)性、市場前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供成都做網(wǎng)站、網(wǎng)站制作 網(wǎng)站設(shè)計制作按需網(wǎng)站策劃,公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),成都品牌網(wǎng)站建設(shè),網(wǎng)絡(luò)營銷推廣,外貿(mào)網(wǎng)站制作,本溪網(wǎng)站建設(shè)費用合理。
**isinstance(object, classinfo) ** # Where object specify name of the object
isinstance()參數(shù):
接受 2 個參數(shù),其中第一個參數(shù)是 string、int、float、long 或自定義類型的對象。
| 參數(shù) | 描述 | 必需/可選 |
|---|---|---|
| 目標(biāo) | 要檢查的對象。 | 需要 |
| 分類(classify) | 類名或類名元組。 | 需要 |
isinstance()返回值
它返回真或假的布爾值。
| 投入 | 返回值 | | 對象是一個實例 | 真實的 | | 對象不是實例 | 錯誤的 | | classinfo 不是類型或類型元組 | 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:使用本機類型處理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()的工作方式另一個示例?
# 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 網(wǎng)站題目:創(chuàng)新互聯(lián)Python教程:Pythonisinstance()
本文路徑:http://www.dlmjj.cn/article/dpdiphe.html


咨詢
建站咨詢
