1. Order Introduction
Recently learned and used a Python built-in function dir, first help:
Copy Code code as follows:
>>> Help (dir)
Help on built-in function dir in module __builtin__:
Dir ()
Dir ([object])-> list of strings
Return a 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, it's easy to think that Dir lists the properties of the specified object or class.
2. Examples
The following is a simple test:
Copy Code code as follows:
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:
Copy Code code as follows:
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__ ', ' __ge ' T__ ', ' __getattribute__ ', ' __gt__ ', ' __hash__ ', ' __init__ ', ' __le__ ', ' __lt__ ', ' __ne__ ', ' __new__ ', ' __reduce__ ', ' __ Reduce_ex__ ', ' __repr__ ', ' __self__ ', ' __setattr__ ', ' __sizeof__ ', ' __str__ ', ' __subclasshook__ '
3. Use Dir to find all classes under module
The original purpose of using this function is to find the class name of the implementation in a module, which can be easily implemented by this function.
For example, to save the above test program as a.py, and then build a test program, the contents are as follows:
Copy Code code as follows:
Import A
if __name__ = = ' __main__ ':
Print ("dir module A:", dir (a))
The results are as follows:
Copy Code code as follows:
Dir module A: [' A ', ' A1 ', ' __builtins__ ', ' __doc__ ', ' __file__ ', ' __name__ ', ' __package__ ']
You can see both Class A and A1 can be found.
4. How to find the class under the current module
This is a problem for a long time, and did not search for a detailed solution, the following is my centralized implementation method.
4.1. Method One: Directly below the module, call
For example, add a line at the bottom of the above a.py, you can use Selfdir in subsequent code to find the class under the current module, the modified code is as follows:
Copy Code code as follows:
Class A:
def a (self):
Pass
Class A1 (A):
DEF a1 (self):
Pass
Curmoduledir=dir () # get dir of the 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 Two: Import current module
refer to the current module as the other import, the code is as follows:
Copy Code code as follows:
# a.py
Import A as this # import the 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. Method Three: Find module based on the module name, then call dir
We know that module has a property __name__ display the module name, how can you find a module object based on the module name? can use Sys.modules. The code is as follows:
Copy Code code 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__ to get the current module object, and then use the object to get dir