Python standard library: Built-in function dir ([object])

Source: Internet
Author: User
Tags list of attributes

This function is used to display a list of properties in the current scope, or a list of attributes for the parameter object. When there is no Parameter object, displays a list of properties that are in the current scope, and if there is a Parameter object, it displays a list of properties owned by the object. This function, when displaying the object's list of properties , checks to see if the object exists with the __dir__ () function, invokes the function if it exists, and displays a list of the properties returned by the function. Of course users can also use the __getattr__ () or __getattribute__ () function to customize the display of properties. If the object does not have a __dir__ () function defined, try to get a list of properties from the object's __dict__ property, so that the properties may not be as accurate as they appear. Some properties may not be shown, particularly the way the __getattr__ () function is used to get properties.

The mechanism for designing this function is to display different attribute lists for different types of objects, but the main principle is to show the most important attributes, rather than the full information display aspect as the focus.

1) If the object is a module object, the property list is displayed primarily as a list of the name attributes of the object.

2) If the object is a type or class object, the primary display is the property name, and the property name of the base class is displayed recursively.

3) If the object is another type, the main display property name, class property name, recursive to the base class property name.

Example:

#dir () Function Import structprint (dir (struct)) print (dir ()) L = [1, 2, 3]print (dir (l)) print (dir (' A ')) class Shape:     def __ Dir__ (self):         return ["area", ' perimeter ', ' location ']s = Shape () print (dir (s))

The resulting output is as follows:

[' Struct ', ' __all__ ', ' __builtins__ ', ' __cached__ ', ' __doc__ ', ' __file__ ', ' __loader__ ', ' __name__ ', ' __package__ ', ' __ Spec__ ', ' _clearcache ', ' calcsize ', ' Error ', ' Iter_unpack ', ' pack ', ' pack_into ', ' unpack ', ' unpack_from '

[' __builtins__ ', ' __cached__ ', ' __doc__ ', ' __file__ ', ' __loader__ ', ' __name__ ', ' __package__ ', ' __spec__ ', ' struct ']

[' __add__ ',  ' __class__ ',  ' __contains__ ',  ' __delattr__ ',  ', __delitem__ ',  ' __dir__ ',   ' __doc__ ',  ' __eq__ ',  ' __format__ ',  ' __ge__ ',  ' __getattribute__ ',  ' __getitem__ ',   ' __gt__ ',  ' __hash__ ',  ' __iadd__ ',  ' __imul__ ',  ' __init__ ',  ' __iter__ ',  ' __ le__ ',  ' __len__ ',  ' __lt__ ',  ' __mul__ ',  ', ' __ne__ ',  ' __new__ ',  ' __reduce__ ',  ' __reduce_ex__ ',  ' __repr__ ',  ' __reversed__ ',  ', __rmul__ ',  ' __setattr__ ',  ' __setitem__ ',   ' __sizeof__ ',  ' __str__ ',  ' __subclasshook__ ',  ' append ',  ' clear ',  ' copy ',  ' Count ',  ' extend ',  ' index ',  ' Insert ',  ' Pop ',  ' remove ',  ' reverse ',  ' sort ']

[' __add__ ',  ' __class__ ',  ' __contains__ ',  ' __delattr__ ',  ' __dir__ ',  ' __doc__ ',  ' _ _eq__ ',  ' __format__ ',  ' __ge__ ',  ', __getattribute__ ',  ' __getitem__ ',  ' __getnewargs__ ',   ' __gt__ ',  ' __hash__ ',  ' __init__ ',  ' __iter__ ',  ' __le__ ',  ' __len__ ',  ' __lt__ ',  ' __mod__ ',  ' __mul__ ',  ' __ne__ ',  ', ' __new__ ',  ', ' __reduce__ ',  ' __reduce_ex__ ',   ' __repr__ ',  ' __rmod__ ',  ' __rmul__ ',  ' __setattr__ ',  ' __sizeof__ ',  ' __str__ ',   ' __subclasshook__ ',  ' capitalize ',  ' casefold ',  ' center ',  ' count ',  ' encode ',   ' endswith ',  ' expandtabs ',  ' find ',  ' format ',  ' format_map ',  ' index ',  ' Isalnum ',  ' isalpha ',  ' isdecimal ',  ' isdigit ', ',  ' isidentifier ',  ' islower ',  ' IsNumeric ',  ' isprintable ',  ' isspace ',  ', Istitle ',  ' isupper ',  ' join ',  ' ljust ',   ' Lower ',  ' lstrip ',  ' maketRans ',  ' partition ',  ' replace ',  ' rfind ',  ' rindex ',  ', ' rjust ',  ' rpartition ',  ' Rsplit ',  ' Rstrip ',  ' Split ',,  ' splitlines ',  ' startswith ', ',  ', ' Strip ',  ' swapcase ',   ' title ',  ' Translate ',  ' upper ',  ' Zfill ']

[' Area ', ' location ', ' perimeter ']


Python standard library: Built-in function dir ([object])

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.