欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python內(nèi)置函數(shù)dir詳解

 更新時(shí)間:2015年04月14日 09:51:01   投稿:junjie  
這篇文章主要介紹了Python內(nèi)置函數(shù)dir詳解,本文講解了命令介紹、使用實(shí)例、使用dir查找module下的所有類、如何找到當(dāng)前模塊下的類等內(nèi)容,需要的朋友可以參考下

1.命令介紹

最近學(xué)習(xí)并使用了一個(gè)python的內(nèi)置函數(shù)dir,首先help一下:

復(fù)制代碼 代碼如下:

>>> 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,可以簡單的認(rèn)為dir列出指定對(duì)象或類的屬性。
2.實(shí)例
下面是一個(gè)簡單的測試:
復(fù)制代碼 代碼如下:

 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))


測試結(jié)果:
復(fù)制代碼 代碼如下:

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下的所有類
最初使用這個(gè)函數(shù)的初衷,就是在一個(gè)module中查找實(shí)現(xiàn)的類名,通過該函數(shù)可以很容易的實(shí)現(xiàn)。
比如把上面的測試程序保存為A.py,再建一個(gè)測試程序,內(nèi)容如下:
復(fù)制代碼 代碼如下:

import A

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


結(jié)果如下:
復(fù)制代碼 代碼如下:

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

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

4.如何找到當(dāng)前模塊下的類

這是一個(gè)煩惱較長時(shí)間的一個(gè)問題,也沒有搜到詳細(xì)的解決方法,下面是我的集中實(shí)現(xiàn)方法。

4.1.方法一:在module下面直接調(diào)用

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

復(fù)制代碼 代碼如下:

 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一樣引用,代碼如下:

復(fù)制代碼 代碼如下:

 # 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名稱查找module,然后調(diào)用dir
我們知道m(xù)odule下面有個(gè)屬性__name__顯示module名稱,怎么能夠根據(jù)module名稱來查找module對(duì)象呢?可以借助sys.modules。代碼如下:
復(fù)制代碼 代碼如下:

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 OpenCV直方圖均衡化詳解

    Python OpenCV直方圖均衡化詳解

    本文中將介紹如何使用OpenCV函數(shù)執(zhí)行直方圖均衡,并將其應(yīng)用于灰度和彩色圖像,以及將亮度歸一化并提高圖像的對(duì)比度。感興趣的小伙伴可以了解一下
    2022-02-02
  • Python利用yagmail實(shí)現(xiàn)自動(dòng)化郵件營銷

    Python利用yagmail實(shí)現(xiàn)自動(dòng)化郵件營銷

    在Python編程中,發(fā)送電子郵件是一個(gè)常見的需求,yagmail是一個(gè)Python庫,旨在簡化通過SMTP協(xié)議發(fā)送電子郵件的過程,所以本文小編就來和大家聊聊Python如何利用yagmail實(shí)現(xiàn)自動(dòng)化郵件營銷吧
    2025-05-05
  • Python Matplotlib繪制條形圖的全過程

    Python Matplotlib繪制條形圖的全過程

    Python畫圖主要用到matplotlib這個(gè)庫,具體來說是pylab和pyplot這兩個(gè)子庫,這兩個(gè)庫可以滿足基本的畫圖需求,下面這篇文章主要給大家介紹了關(guān)于Python Matplotlib繪制條形圖的相關(guān)資料,需要的朋友可以參考下
    2021-10-10
  • Python從MySQL數(shù)據(jù)庫中面抽取試題,生成試卷

    Python從MySQL數(shù)據(jù)庫中面抽取試題,生成試卷

    這篇文章主要介紹了Python如何從MySQL數(shù)據(jù)庫中面抽取試題,生成試卷,幫助大家更好的理解和使用python,感興趣的朋友可以了解下
    2021-01-01
  • Python的f-string使用技巧

    Python的f-string使用技巧

    Python很早就引入了一種稱為 f-string 的字符串格式化方法,它代表格式化字符串字面值,本文主要介紹了Python的f-string使用技巧,具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-01-01
  • python檢測遠(yuǎn)程端口是否打開的方法

    python檢測遠(yuǎn)程端口是否打開的方法

    這篇文章主要介紹了python檢測遠(yuǎn)程端口是否打開的方法,實(shí)例分析了Python基于socket檢測端口的技巧,需要的朋友可以參考下
    2015-03-03
  • Python使用post及get方式提交數(shù)據(jù)的實(shí)例

    Python使用post及get方式提交數(shù)據(jù)的實(shí)例

    今天小編就為大家分享一篇關(guān)于Python使用post及get方式提交數(shù)據(jù)的實(shí)例,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧
    2019-01-01
  • Python?Web后端開發(fā)中的增查改刪處理

    Python?Web后端開發(fā)中的增查改刪處理

    這篇文章主要介紹了Python?Web后端開發(fā)中的增查改刪處理,文章圍繞主題?展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下
    2022-08-08
  • python定時(shí)檢查啟動(dòng)某個(gè)exe程序適合檢測exe是否掛了

    python定時(shí)檢查啟動(dòng)某個(gè)exe程序適合檢測exe是否掛了

    定時(shí)檢查啟動(dòng)某個(gè)exe程序這種情況下適合檢測某個(gè)exe程序是否掛了,感興趣的朋友可以了解下,希望本文對(duì)你有所幫助
    2013-01-01
  • Python 實(shí)現(xiàn)進(jìn)度條的六種方式

    Python 實(shí)現(xiàn)進(jìn)度條的六種方式

    這篇文章主要介紹了Python 實(shí)現(xiàn)進(jìn)度條的六種方式,幫助大家更好的理解和使用python,感興趣的朋友可以了解下
    2021-01-01

最新評(píng)論