新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
創(chuàng)新互聯(lián)Python教程:Pythondir()
dir 函數(shù)將一個對象作為輸入,并返回該對象的所有屬性。屬性以列表的形式返回。

網(wǎng)站建設哪家好,找成都創(chuàng)新互聯(lián)公司!專注于網(wǎng)頁設計、網(wǎng)站建設、微信開發(fā)、成都微信小程序、集團企業(yè)網(wǎng)站建設等服務項目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了開原免費建站歡迎大家使用!
**dir(object)** # object is any python object
目錄()參數(shù):
dir()函數(shù)將一個對象作為參數(shù)。內(nèi)置對象和用戶定義對象可以作為參數(shù)傳遞。即使沒有傳遞參數(shù),它也不會拋出錯誤。
| 參數(shù) | 描述 | 必需/可選 |
|---|---|---|
| 目標 | 要返回其屬性的任何 Python 對象 | 可選擇的 |
目錄()返回值
函數(shù)返回傳遞的對象的所有屬性,內(nèi)置屬性也返回。如果沒有傳遞參數(shù),則返回當前本地范圍內(nèi)的名稱。
| 投入 | 返回值 | | 沒有人 | 返回當前本地范圍內(nèi)的名稱列表。 | | 目標 | 返回傳遞的對象的屬性 |
Python 中dir()方法的示例
示例 1:在對象中傳遞構(gòu)造函數(shù)
letters = {'a':1, 'b':2, 'c':3}
# Dictionary is a built in object in python
print(dir(letters))
輸出:
[' class ', ' contains ', ' delattr ', ' delitem ', ' dir ', ' doc ', ' eq ',
' format ', ' ge ', ' getattribute ', ' getitem ', ' gt ', ' hash ', ' init ',
' init_subclass ', ' iter ', ' le ', ' len ', ' lt ', ' ne ', ' new ', ' reduce ', ' reduce_ex ', ' repr ', ' setattr ', ' setitem ', ' sizeof ', ' str ',
' subclasshook ', 'clear', 'copy', 'fromkeys', 'get', 'items', 'keys', 'pop', 'popitem', 'setdefault', 'update', 'values'] 示例 2:不傳遞參數(shù)
print(dir())
輸出:
[' annotations ', ' builtins ', ' cached ', ' doc ', ' file ', ' loader ', ' name ', ' package ', ' spec '] 示例 3:傳遞用戶定義的對象
class Student:
name = "Ram"
age = 22
course = "Bachelors"
Student1 = Student()
print(dir(Student1))
輸出:
[' class ', ' delattr ', ' dict ', ' dir ', ' doc ', ' eq ', ' format ', ' ge ', ' getattribute ', ' gt ', ' hash ', ' init ', ' init_subclass ', ' le ', ' lt ',
' module ', ' ne ', ' new ', ' reduce ', ' reduce_ex ', ' repr ', ' setattr ', ' sizeof ', ' str ', ' subclasshook ', ' weakref ', 'age', 'course', 'name']
# The built in attributes of class is also returned along with the user defined attributes
網(wǎng)站名稱:創(chuàng)新互聯(lián)Python教程:Pythondir()
網(wǎng)頁URL:http://m.fisionsoft.com.cn/article/dpcgcpo.html


咨詢
建站咨詢
