>>help(dir)Helponbuilt-infunctiondirinmodule__builtin__:dir()dir([object])->listofstringsReturnanalphabetizedlistofnamescomprising(someof)theattributesofthegivenobject,andofattribu" />

亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

Python內置函數dir詳解

系統 1825 0

1.命令介紹

最近學習并使用了一個python的內置函數dir,首先help一下:

復制代碼 代碼如下:

>>> help(dir)
Help on built-in function dir in module __builtin__:


dir()
??? dir([object]) -> list of strings


??? Return an alphabetized list of names comprising (some of) the attributes
??? of the given object, and of attributes reachable from it:


??? No argument:? the names in the current scope.
??? Module object:? the module attributes.
??? Type or class object:? its attributes, and recursively the attributes of
??????? its bases.
??? Otherwise:? its attributes, its class's attributes, and recursively the
??????? attributes of its class's base classes.


通過help,可以簡單的認為dir列出指定對象或類的屬性。
2.實例
下面是一個簡單的測試:
復制代碼 代碼如下:

?class A:
???? def a(self):
???????? pass
?
?
?class A1(A):
??? def a1(self):
??????? pass


if __name__ == '__main__':
??? print("dir without arguments:", dir())
??? print("dir class A:", dir(A))
??? print("dir class A1:", dir(A1))
??? a = A1()
??? print("dir object a(A1):", dir(a))
??? print("dir function a.a:", dir(a.a))


測試結果:
復制代碼 代碼如下:

dir without arguments: ['A', 'A1', '__builtins__', '__doc__', '__file__', '__name__', '__package__']
dir class A: ['__class__', '__delattr__', '__dict__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'a']
dir class A1: ['__class__', '__delattr__', '__dict__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'a', 'a1']
dir object a(A1): ['__class__', '__delattr__', '__dict__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'a', 'a1']
dir function a.a: ['__call__', '__class__', '__delattr__', '__doc__', '__eq__', '__format__', '__func__', '__ge__', '__get__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__self__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__']

3.使用dir查找module下的所有類
最初使用這個函數的初衷,就是在一個module中查找實現的類名,通過該函數可以很容易的實現。
比如把上面的測試程序保存為A.py,再建一個測試程序,內容如下:
復制代碼 代碼如下:

import A

if __name__ == '__main__':
??? print("dir module A:", dir(A))


結果如下:
復制代碼 代碼如下:

dir module A: ['A', 'A1', '__builtins__', '__doc__', '__file__', '__name__', '__package__']

可以看出class A和A1都能夠找到。

4.如何找到當前模塊下的類

這是一個煩惱較長時間的一個問題,也沒有搜到詳細的解決方法,下面是我的集中實現方法。

4.1.方法一:在module下面直接調用

比如在上面的A.py最下面添加一行,即可在后續的代碼中可以使用selfDir來查找當前的module下的類,修改后的代碼如下:

復制代碼 代碼如下:

?class A:
???? def a(self):
???????? pass
?
?class A1(A):
???? def a1(self):
???????? pass
?
?curModuleDir=dir()? # get dir of current file(module)

if __name__ == '__main__':
??? print("dir without arguments:", dir())
??? print("dir class A:", dir(A))
??? print("dir class A1:", dir(A1))
??? a = A1()
??? print("dir object a(A1):", dir(a))
??? print("dir function a.a:", dir(a.a))
??? print("dir current file:", curModuleDir)

4.2.方法二:import當前module
把當前module和別的import一樣引用,代碼如下:

復制代碼 代碼如下:

?# A.py
?import A as this # import current module
?
?class A:
???? def a(self):
???????? pass
?
?class A1(A):
???? def a1(self):
??????? pass

if __name__ == '__main__':
??? print("dir without arguments:", dir())
??? print("dir class A:", dir(A))
??? print("dir class A1:", dir(A1))
??? a = A1()
??? print("dir object a(A1):", dir(a))
??? print("dir function a.a:", dir(a.a))
??? print("dir current file:", dir(this))


4.3.方法三:根據module名稱查找module,然后調用dir
我們知道module下面有個屬性__name__顯示module名稱,怎么能夠根據module名稱來查找module對象呢?可以借助sys.modules。代碼如下:
復制代碼 代碼如下:

import sys

class A:
??? def a(self):
??????? pass

class A1(A):
??? def a1(self):
??????? pass

if __name__ == '__main__':
??? print("dir without arguments:", dir())
??? print("dir class A:", dir(A))
??? print("dir class A1:", dir(A1))
??? a = A1()
??? print("dir object a(A1):", dir(a))
??? print("dir function a.a:", dir(a.a))
??? print("dir current file:", dir(sys.modules[__name__])) # 使用__name__獲取當前module對象,然后使用對象獲得dir


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 福利视频第一页 | 国产欧美精品一区二区色综合 | 国产精品久久久久影视青草 | 国产毛片一级国语版 | 久久久久综合 | 成人网久久 | 青草青青在线观看免费视频 | 亚洲国产精品久久久久久网站 | 精品视频亚洲 | 欧美成人一区二区 | 很很鲁在线视频播放影院 | 大陆一级毛片免费视频观看i | 在线观看国产精品入口 | 综合啪啪 | 亚洲精品动漫3d一区二区 | 亚洲欧美国产另类 | 99在线播放 | 日本精品久久久中文字幕 | 亚洲女人国产香蕉久久精品 | 日本一级欧美一级中文 | 在线观看国产久青草 | 国产美女精品视频 | 成人欧美一区二区三区白人 | 亚洲视频色 | 天天干在线观看 | 亚洲精品免费日日日夜夜夜夜 | 国产免费一级片 | 国产 欧美 在线 | 日韩毛片在线影视 | 亚洲欧美另类在线 | 国产中文字幕免费观看 | 免费日本黄色网址 | 欧美经典人人爽人人爽人人片 | 国内精品久久久久鸭 | 2021中文字幕亚洲精品 | 久久国产精品一区二区三区 | 免费看日韩欧美一级毛片 | 性欧美xo视频在线观看 | 国产精品永久免费视频观看 | 久色网站| 亚洲欧美专区 |