• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            posts - 26, comments - 2, trackbacks - 0, articles - 0
              C++博客 :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

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

            Posted on 2009-10-16 20:32 小夜 閱讀(3129) 評論(0)  編輯 收藏 引用 所屬分類: [52] Python
            1.命令介紹
            最近學(xué)習(xí)并使用了一個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.

            通過help,可以簡單的認(rèn)為dir列出指定對象或類的屬性。
            2.實(shí)例
            下面是一個簡單的測試:
             1class A:
             2    def a(self):
             3        pass
             4
             5
             6class A1(A):
             7    def a1(self):
             8        pass
             9
            10
            11if __name__ == '__main__':
            12    print("dir without arguments:", dir())
            13    print("dir class A:", dir(A))
            14    print("dir class A1:", dir(A1))
            15    a = A1()
            16    print("dir object a(A1):", dir(a))
            17    print("dir function a.a:", dir(a.a))
            18
            測試結(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下的所有類
            最初使用這個函數(shù)的初衷,就是在一個module中查找實(shí)現(xiàn)的類名,通過該函數(shù)可以很容易的實(shí)現(xiàn)。
            比如把上面的測試程序保存為A.py,再建一個測試程序,內(nèi)容如下:
            1import A
            2
            3
            4if __name__ == '__main__':
            5    print("dir module A:", dir(A))
            6
            結(jié)果如下:
            dir module A: ['A''A1''__builtins__''__doc__''__file__''__name__''__package__']
            可以看出class A和A1都能夠找到。
            4.如何找到當(dāng)前模塊下的類
            這是一個煩惱較長時間的一個問題,也沒有搜到詳細(xì)的解決方法,下面是我的集中實(shí)現(xiàn)方法。
            4.1.方法一:在module下面直接調(diào)用
            比如在上面的A.py最下面添加一行,即可在后續(xù)的代碼中可以使用selfDir來查找當(dāng)前的module下的類,修改后的代碼如下:
             1class A:
             2    def a(self):
             3        pass
             4
             5class A1(A):
             6    def a1(self):
             7        pass
             8
             9curModuleDir=dir()  # get dir of current file(module) 
            10
            11if __name__ == '__main__':
            12    print("dir without arguments:", dir())
            13    print("dir class A:", dir(A))
            14    print("dir class A1:", dir(A1))
            15    a = A1()
            16    print("dir object a(A1):", dir(a))
            17    print("dir function a.a:", dir(a.a))
            18    print("dir current file:", curModuleDir)
            19

            4.2.方法二:import當(dāng)前module
            把當(dāng)前module和別的import一樣引用,代碼如下:
             1# A.py
             2import A as this # import current module
             3
             4class A:
             5    def a(self):
             6        pass
             7
             8class A1(A):
             9    def a1(self):
            10        pass
            11
            12if __name__ == '__main__':
            13    print("dir without arguments:", dir())
            14    print("dir class A:", dir(A))
            15    print("dir class A1:", dir(A1))
            16    a = A1()
            17    print("dir object a(A1):", dir(a))
            18    print("dir function a.a:", dir(a.a))
            19    print("dir current file:", dir(this))
            4.3.方法三:根據(jù)module名稱查找module,然后調(diào)用dir
            我們知道m(xù)odule下面有個屬性__name__顯示module名稱,怎么能夠根據(jù)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__獲取當(dāng)前module對象,然后使用對象獲得dir

            7国产欧美日韩综合天堂中文久久久久| 日韩va亚洲va欧美va久久| 伊人精品久久久久7777| 亚洲国产精品成人久久| 嫩草影院久久国产精品| 亚洲伊人久久成综合人影院 | 久久精品国产亚洲AV影院 | 久久国产精品77777| 国产精品久久久久AV福利动漫| 丁香久久婷婷国产午夜视频| 久久99久久无码毛片一区二区| 99久久精品国产一区二区| 91精品国产色综久久| 久久午夜福利无码1000合集| 伊人色综合久久天天| 久久香综合精品久久伊人| 狠狠色综合网站久久久久久久 | 久久综合中文字幕| 欧美日韩精品久久免费| 久久久久久av无码免费看大片| 亚洲va久久久噜噜噜久久 | 久久久老熟女一区二区三区| 青青草国产97免久久费观看| 一级做a爱片久久毛片| 久久久久高潮毛片免费全部播放| 欧美无乱码久久久免费午夜一区二区三区中文字幕 | 国产亚洲色婷婷久久99精品| 久久久久av无码免费网| 久久亚洲精品无码播放| 久久成人精品| 精品久久久久久99人妻| 婷婷综合久久中文字幕| 91亚洲国产成人久久精品网址 | 精品无码久久久久国产动漫3d| 久久综合九色综合久99| 久久午夜无码鲁丝片午夜精品| 中文字幕亚洲综合久久2| 亚洲午夜久久久精品影院| 国产激情久久久久影院老熟女免费 | 狠狠色丁香久久婷婷综合_中| 久久久91人妻无码精品蜜桃HD|