Python-dir () function

Source: Internet
Author: User
Tags list of attributes

Dir () function

English Description:

You can use the built-in Dir function to list the identifier for a module definition. Identifiers have functions, classes, and variables.

When you provide a module name for Dir (), it returns a list of the name of the module definition. If no argument is supplied, it returns a list of names defined in the current module.

First, let's take a look at using dir on the input sys module. We see that it contains a large list of attributes.

Next, we don't use it to pass parameters to the Dir function--by default, it returns the list of properties for the current module. Note that the input module is also part of the list.

To observe the function of Dir, we define a new variable A and assign it a value, and then we examine dir, and we observe that the same value is added to the list. We use the DEL statement to delete the variable/attribute in the current module, which is once again reflected in the output of Dir.

A note about Del--this statement is used to delete a variable/name after it is run. In this example, Del A, you will no longer be able to use the variable a--it as if it had never existed.

Version:

This function is supported in each version and is still available in Python3.

code example:

>>> Import struct>>> dir ()   # Show the names in the module namespace[' __builtins__ ', ' __doc__ ', ' __na me__ ', ' struct ']>>> dir (struct)   # show the names in the struct module[' struct ', ' __builtins__ ', ' __doc__ ', ' _ _file__ ', ' __name__ ', ' __package__ ', ' _clearcache ', ' calcsize ', ' Error ', ' pack ', ' pack_into ', ' unpack ', ' Unpack_from ']& Gt;>> class Shape (object):        def __dir__ (self):            return ["area", ' perimeter ', ' location ']>>> s = Shape () >>> dir (s) [' area ', ' perimeter ', ' location ']

English Description:

Dir ([Object])

Without arguments, return the list of names in the current local scope. With an argument, attempt to return a list of valid attributes for that object.

If the object has a method named __dir__ (), this method would be called and must return the list of attributes. This allows objects, implement a custom __getattr__ () or __getattribute__ () function to customize the by Dir () report s their attributes.

If The object does not provide __dir__ (), the function tries its best to gather information from the object ' s __dict__ att Ribute, if defined, and from its type object. The resulting list is not necessarily complete, and could be inaccurate if the object has a custom __getattr__ ().

The default dir () mechanism behaves differently with different types of objects, as it attempts-produce the most Releva NT, rather than complete, information:

If the object is a module object, the list contains the names of the module ' s attributes.

If The object is a type or class object, the list contains the names of its attributes, and recursively of the attributes of its bases.

Otherwise, the list contains the object ' s attributes ' names, the names of its class ' s attributes, and recursively of the A Ttributes of its class ' s base classes.

  • 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.