Python built-in functions dir and pythondir

Source: Internet
Author: User

Python built-in functions dir and pythondir

1. Command Introduction

Recently I learned and used a python built-in function dir. First, help:
Copy codeThe 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:
Copy codeThe 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:
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 _', '_ 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:
Copy codeThe Code is as follows:
Import

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

The result is as follows:
Copy codeThe 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:
Copy codeThe 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:
Copy codeThe 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:
Copy codeThe 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

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.