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

欧美三区_成人在线免费观看视频_欧美极品少妇xxxxⅹ免费视频_a级毛片免费播放_鲁一鲁中文字幕久久_亚洲一级特黄

Python內置函數dir詳解

系統 1705 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條評論
主站蜘蛛池模板: 国产精品美女网站在线看 | 日本一级成人毛片免费观看 | 国产噜噜在线视频观看 | 色客成人网| 亚洲成人一区二区三区 | a一级黄色片 | 亚洲一区精品在线 | 午夜刺激视频 | 国产伦精品一区二区三区高清 | 日韩亚洲一区二区三区 | 色哟哟哟在线精品观看视频 | 久久一区 | 91在线成人| 免费国产自久久久久三四区久久 | 日韩欧美一级精品久久 | 日韩av不卡在线 | 浮力影院最新网址 | 欧洲另类一二三四区 | 一区二区三区视频在线 | 男人的午夜天堂 | www.小视频| 91精品国产综合久久久久久丝袜 | 色黄视频在线观看 | 国产精品久久久久无码av | 午夜视频在线观看www中文 | 在线天堂中文在线资源网 | 一级看片免费视频 | 日韩精品一区二区电影 | 午夜免费看片 | 亚洲精品人成网在线播放影院 | 免费黄色在线观看 | 97se亚洲综合在线韩国专区福利 | www国产成人免费观看视频,深夜成人网 | 九九热视| 国产精品欧美一区二区在线看 | 久久色伦理资源站 | 免费成人电影在线 | 亚洲第一第二区 | 日本免费在线一区 | caoliusequ| 国产乱码精品1区2区3区 |