Python built in function dir

  • 2020-05-09 18:49:10
  • OfStack

1. Command introduction

Recently, I learned and used one of python's built-in functions, dir. First, help1:


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

With help, you can simply think of dir as listing the properties of a specified object or class.
Example 2.
Here's a simple test:

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

Test results:

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. Use dir to find all the classes under module
The original intention of using this function is to find the class name of the implementation in 1 module, which can be easily implemented through this function.
For example, save the above test program as A.py, and then build another test program. The content is as follows:

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

The results are as follows:

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

You can see that both class A and A1 can be found.

4. How to find the class under the current module

This is a problem that has been bothering for a long time, and no detailed solution has been found. The following is my centralized implementation method.

Method 1: called directly under module

For example, if you add 1 line at the bottom of A.py above, you can use selfDir to find the current module class in the following code. The modified code is as follows:


 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. Method 2: import current module
Reference the current module and other import1 samples, the code is as follows:


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

Method 3: look up module by module name, then call dir
We know that there is a property below module showing the module name, how can we find the module object according to the module name? You can use sys.modules. The code is as follows:

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__])) # use __name__ Gets the current module Object, which is then obtained using the object dir


Related articles: