Python內(nèi)置函數(shù)dir詳解
1.命令介紹
最近學(xué)習(xí)并使用了一個(gè)python的內(nèi)置函數(shù)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.
通過(guò)help,可以簡(jiǎn)單的認(rèn)為dir列出指定對(duì)象或類(lèi)的屬性。
2.實(shí)例
下面是一個(gè)簡(jiǎn)單的測(cè)試:
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))
測(cè)試結(jié)果:
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下的所有類(lèi)
最初使用這個(gè)函數(shù)的初衷,就是在一個(gè)module中查找實(shí)現(xiàn)的類(lèi)名,通過(guò)該函數(shù)可以很容易的實(shí)現(xiàn)。
比如把上面的測(cè)試程序保存為A.py,再建一個(gè)測(cè)試程序,內(nèi)容如下:
import A
if __name__ == '__main__':
print("dir module A:", dir(A))
結(jié)果如下:
dir module A: ['A', 'A1', '__builtins__', '__doc__', '__file__', '__name__', '__package__']
可以看出class A和A1都能夠找到。
4.如何找到當(dāng)前模塊下的類(lèi)
這是一個(gè)煩惱較長(zhǎng)時(shí)間的一個(gè)問(wèn)題,也沒(méi)有搜到詳細(xì)的解決方法,下面是我的集中實(shí)現(xiàn)方法。
4.1.方法一:在module下面直接調(diào)用
比如在上面的A.py最下面添加一行,即可在后續(xù)的代碼中可以使用selfDir來(lái)查找當(dāng)前的module下的類(lèi),修改后的代碼如下:
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當(dāng)前module
把當(dāng)前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.方法三:根據(jù)module名稱(chēng)查找module,然后調(diào)用dir
我們知道m(xù)odule下面有個(gè)屬性__name__顯示module名稱(chēng),怎么能夠根據(jù)module名稱(chēng)來(lái)查找module對(duì)象呢?可以借助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__獲取當(dāng)前module對(duì)象,然后使用對(duì)象獲得dir
相關(guān)文章
Python利用yagmail實(shí)現(xiàn)自動(dòng)化郵件營(yíng)銷(xiāo)
在Python編程中,發(fā)送電子郵件是一個(gè)常見(jiàn)的需求,yagmail是一個(gè)Python庫(kù),旨在簡(jiǎn)化通過(guò)SMTP協(xié)議發(fā)送電子郵件的過(guò)程,所以本文小編就來(lái)和大家聊聊Python如何利用yagmail實(shí)現(xiàn)自動(dòng)化郵件營(yíng)銷(xiāo)吧2025-05-05Python Matplotlib繪制條形圖的全過(guò)程
Python畫(huà)圖主要用到matplotlib這個(gè)庫(kù),具體來(lái)說(shuō)是pylab和pyplot這兩個(gè)子庫(kù),這兩個(gè)庫(kù)可以滿(mǎn)足基本的畫(huà)圖需求,下面這篇文章主要給大家介紹了關(guān)于Python Matplotlib繪制條形圖的相關(guān)資料,需要的朋友可以參考下2021-10-10Python從MySQL數(shù)據(jù)庫(kù)中面抽取試題,生成試卷
這篇文章主要介紹了Python如何從MySQL數(shù)據(jù)庫(kù)中面抽取試題,生成試卷,幫助大家更好的理解和使用python,感興趣的朋友可以了解下2021-01-01python檢測(cè)遠(yuǎn)程端口是否打開(kāi)的方法
這篇文章主要介紹了python檢測(cè)遠(yuǎn)程端口是否打開(kāi)的方法,實(shí)例分析了Python基于socket檢測(cè)端口的技巧,需要的朋友可以參考下2015-03-03Python使用post及get方式提交數(shù)據(jù)的實(shí)例
今天小編就為大家分享一篇關(guān)于Python使用post及get方式提交數(shù)據(jù)的實(shí)例,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-01-01Python?Web后端開(kāi)發(fā)中的增查改刪處理
這篇文章主要介紹了Python?Web后端開(kāi)發(fā)中的增查改刪處理,文章圍繞主題?展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-08-08python定時(shí)檢查啟動(dòng)某個(gè)exe程序適合檢測(cè)exe是否掛了
定時(shí)檢查啟動(dòng)某個(gè)exe程序這種情況下適合檢測(cè)某個(gè)exe程序是否掛了,感興趣的朋友可以了解下,希望本文對(duì)你有所幫助2013-01-01Python 實(shí)現(xiàn)進(jìn)度條的六種方式
這篇文章主要介紹了Python 實(shí)現(xiàn)進(jìn)度條的六種方式,幫助大家更好的理解和使用python,感興趣的朋友可以了解下2021-01-01