Explanation of the Python built-in function dir

Source: Internet
Author: User
This article mainly introduces the Python built-in function dir. This article describes the command introduction, the use of instances, the use of dir to find all classes under the module, and how to find the classes under the current module, for more information, see 1. Command Introduction

Recently I learned and used a python built-in function dir. first, help:

The code is 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
Its bases.
Otherwise: its attributes, its class's attributes, and recursively
Attributes of its class's base classes.


With help, you can simply think that dir lists the attributes of a specified object or class.
2. instance
The following is a simple test:

The code is as follows:


Class:
Def a (self ):
Pass


Class A1 ():
Def a1 (self ):
Pass


If _ name _ = '_ main __':
Print ("dir without arguments:", dir ())
Print ("dir class A:", dir ())
Print ("dir class A1:", dir (A1 ))
A = A1 ()
Print ("dir object a (A1):", dir ())
Print ("dir function a. a:", dir (a. ))


Test results:

The 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 _', '_ performance_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 _', '_ performance_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 _', '_ performance_ex _', '_ repr __', '_ setattr _', '_ sizeof _', '_ str _', '_ subclasshook _', '_ weakref __', 'A', 'A1']
Dir function. a: ['_ call _', '_ class _', '_ delattr _', '_ doc __', '_ eq _', '_ format _', '_ func _', '_ ge _', '_ get __', '_ getattribute _', '_ gt _', '_ hash _', '_ init _', '_ le __', '_ lt _', '_ ne _', '_ new _', '_ reduce _', '_ performance_ex __', '_ repr _', '_ self _', '_ setattr _', '_ sizeof _', '_ str __', '_ subclasshook _']


3. use dir to find all classes in the module
The original intention of using this function was to find the implemented class name in a module, which can be easily implemented.
For example, save the above test program as A. py and create another test program. The content is as follows:

The code is as follows:


Import

If _ name _ = '_ main __':
Print ("dir module A:", dir ())


The result is as follows:

The code is as follows:


Dir module A: ['A', 'A1', '_ builtins _', '_ doc _', '_ file __', '_ name _', '_ package _']


We 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 plagued for a long time, and no detailed solution has been found. The following is my centralized implementation method.

4. 1. Method 1: directly call

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

The code is as follows:


Class:
Def a (self ):
Pass

Class A1 ():
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 ())
Print ("dir class A1:", dir (A1 ))
A = A1 ()
Print ("dir object a (A1):", dir ())
Print ("dir function a. a:", dir (a. ))
Print ("dir current file:", curModuleDir)

4. 2. Method 2: import the current module
Reference the current module like other imports. the code is as follows:

The code is as follows:


# A. py
Import A as this # import current module

Class:
Def a (self ):
Pass

Class A1 ():
Def a1 (self ):
Pass

If _ name _ = '_ main __':
Print ("dir without arguments:", dir ())
Print ("dir class A:", dir ())
Print ("dir class A1:", dir (A1 ))
A = A1 ()
Print ("dir object a (A1):", dir ())
Print ("dir function a. a:", dir (a. ))
Print ("dir current file:", dir (this ))


4. 3. Method 3: Find the module based on the module name, and then call dir
We know that the module has an attribute _ name _ showing the module name. how can we find the module object based on the module name? You can use sys. modules. The code is as follows:

The code is as follows:


Import sys

Class:
Def a (self ):
Pass

Class A1 ():
Def a1 (self ):
Pass

If _ name _ = '_ main __':
Print ("dir without arguments:", dir ())
Print ("dir class A:", dir ())
Print ("dir class A1:", dir (A1 ))
A = A1 ()
Print ("dir object a (A1):", dir ())
Print ("dir function a. a:", dir (a. ))
Print ("dir current file:", dir (sys. modules [_ name _]) # Use _ name _ to obtain the current module object, and then use the object to get the 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.