Python built-in functions dir explanation

Source: Internet
Author: User
1. Command Introduction

Recently learned and used a python built-in function, dir, first help:
Copy the Code code as follows:


>>> 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 assume that dir lists the properties of the specified object or class.
2. Example
The following is a simple test:
Copy CodeThe code is 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 CodeThe code is 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 intention of using this function is to find the implemented class name 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 CodeThe code is as follows:


Import A

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


The results are as follows:
Copy CodeThe code is 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 for a long time, and did not search for a detailed solution, the following is my centralized implementation method.

4.1. Method One: Call directly below the module

For example, in the above a.py to add a row, you can use Selfdir in subsequent code to find the current module under the class, the modified code is as follows:
Copy the Code code 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 Two: Import current module
Refer to the current module as in the other import code as follows:
Copy the 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 the module by module name and then call dir
We know that the module has a property __name__ shows 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:
Copy CodeThe 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__ to get the current module object and then use the object to get dir

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.