Dir () function

Source: Internet
Author: User
Tags list of attributes
Concise Python tutorial
Chapter 4 modules
Previous Page Dir () function Next Page
Dir () function

You can use the built-indirFunction to list the identifier defined by the module. Identifiers include functions, classes, and variables.

When youdir()When a module name is provided, it returns the list of module-defined names. If no parameter is provided, it returns the name list defined in the current module.

Use the Dir Function

Example 8.4 use the Dir Function

$ python
>>> import sys
>>> dir(sys) # get list of attributes for sys module
['__displayhook__', '__doc__', '__excepthook__', '__name__', '__stderr__',
'__stdin__', '__stdout__', '_getframe', 'api_version', 'argv',
'builtin_module_names', 'byteorder', 'call_tracing', 'callstats',
'copyright', 'displayhook', 'exc_clear', 'exc_info', 'exc_type',
'excepthook', 'exec_prefix', 'executable', 'exit', 'getcheckinterval',
'getdefaultencoding', 'getdlopenflags', 'getfilesystemencoding',
'getrecursionlimit', 'getrefcount', 'hexversion', 'maxint', 'maxunicode',
'meta_path','modules', 'path', 'path_hooks', 'path_importer_cache',
'platform', 'prefix', 'ps1', 'ps2', 'setcheckinterval', 'setdlopenflags',
'setprofile', 'setrecursionlimit', 'settrace', 'stderr', 'stdin', 'stdout',
'version', 'version_info', 'warnoptions']
>>> dir() # get list of attributes for current module
['__builtins__', '__doc__', '__name__', 'sys']
>>>
>>> a = 5 # create a new variable 'a'
>>> dir()
['__builtins__', '__doc__', '__name__', 'a', 'sys']
>>>
>>> del a # delete/remove a name
>>>
>>> dir()
['__builtins__', '__doc__', '__name__', 'sys']
>>>

How it works

First, let's take a look atsysModule usagedir. We can see that it contains a huge list of attributes.

Next, we will notdirUse the function to PASS Parameters -- by default, it returns the attribute list of the current module. Note that the input module is also part of the list.

To observedirTo define a new variable.aAnd assign a value to it, and then testdir, We can see that the above values are added to the list. We usedelStatement to delete the variables/attributes of the current module. This change is again reflected indir.

AboutdelA note -- this statement is used after runningDeleteA variable/name. In this example,del aYou will no longer be able to use variablesa-- It is as if it has never existed before.

Previous Page Level 1 Next Page
Create your own modules Homepage Summary

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.