Python dir () function

Source: Internet
Author: User
Tags list of attributes

You can use the built-in Dir () function to list an identifier for a defined object. For example, for a module, include functions, classes, and variables defined in the module.

When you provide a module name to Dir (), it returns a list of the names defined in that module. When no arguments are provided for it, it returns a list of the names defined in the current module.

An example of the Dir () function:
>>> Import SYS # get a list of properties, here is the list of properties for the SYS module

>>> dir (SYS)
[' __displayhook__ ', ' __doc__ ', ' __excepthook__ ', ' __name__ ', ' __package__ ', ' __stderr__ ', ' __stdin__ ', ' __stdout__ ', ' _clear_type_cache ', ' _compact_freelists ', ' _current_frames ', ' _getframe ', ' api_version ', ' argv ', ' Builtin_module_ ' Names ', ' byteorder ', ' call_tracing ', ' callstats ', ' copyright ', ' displayhook ', ' dllhandle ', ' dont_write_bytecode ', ' exc ' _info ', ' excepthook ', ' exec_prefix ', ' executable ', ' exit ', ' flags ', ' float_info ', ' getcheckinterval ', ' Getdefaultencoding ', ' getfilesystemencoding ', ' getprofile ', ' getrecursionlimit ', ' getrefcount ', ' getsizeof ', ' Gettrace ', ' getwindowsversion ', ' hexversion ', ' Intern ', ' maxsize ', ' maxunicode ', ' meta_path ', ' modules ', ' path ', ' Path_ Hooks ', ' path_importer_cache ', ' platform ', ' prefix ', ' ps1 ', ' ps2 ', ' setcheckinterval ', ' setprofile ', ' Setrecursionlimit ', ' settrace ', ' stderr ', ' stdin ', ' stdout ', ' subversion ', ' Version ', ' Version_info ', ' warnoptions ', ' Winver ']

>>> dir () # Get a list of properties for the current module
[' __builtins__ ', ' __doc__ ', ' __name__ ', ' __package__ ', ' sys ']

>>> A = 5 # Creates a new variable ' a '

>>> dir ()
[' __builtins__ ', ' __doc__ ', ' __name__ ', ' __package__ ', ' a ', ' sys ']

>>> del A # delete/remove a name

>>> dir ()
[' __builtins__ ', ' __doc__ ', ' __name__ ', ' __package__ ', ' sys ']

>>>

How it works:
First, we see that using Dir is used on the Import sys module. We can see the huge list of attributes that the module contains.

We then use the Dir function, which does not pass parameters. By default, it returns a list of the properties of the module. Note that the list of imported modules is still part of this list.

To see that dir is working, we define a new variable, assign it a value, and then check dir, and we find that a variable with the same name is added to the list. We use the DEL statement to remove the variable or property of the current module, and the change in the output of the Del function is reflected again.

A little note about Del-this statement is used to delete a variable/attribute, after the statement runs, here is del A, you can no longer access the variable a--as if it had never existed at all.

Note that the Dir () function works on any object. For example, run dir (' print ') to learn more about the properties of the print function, or run dir (str) to learn more about the properties of the Str class.

Python dir () function

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.