Python standard library: built-in functions dir ([object]), pythondir

Source: Internet
Author: User

Python standard library: built-in functions dir ([object]), pythondir

This function is used to display the attribute list in the current scope or the attribute list of the parameter object. If no parameter object exists, the attribute list of the current scope is displayed. If a parameter object exists, the attribute list of this object is displayed. When displaying the attribute list of an object, this function checks whether the object has the _ dir _ () function. If yes, call this function and display the attribute list returned by this function. Of course, you can also use the _ getattr _ () or _ getattribute _ () function to customize attribute display. If the object does not have the _ dir _ () function, try to obtain the attribute list from the _ dict _ attribute of the object, so that the display attribute is not so accurate, some attributes may not be displayed, especially the _ getattr _ () function is used to obtain the attributes.

The function design mechanism is as follows: different attribute lists are displayed for different types of objects, but the main principle is to display the most important attributes rather than all information as the focus.

1) if the object is a module object, the attribute list displays the name attribute list of the object.

2) if the object is a type or class object, the attribute name is displayed mainly, and the attribute name of the base class is displayed recursively.

3) if the object is of another type, the attribute name and class attribute name are displayed, recursively returning to the base class attribute 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 ['region', 'Perimeter ', 'location'] s = Shape () print (dir (s ))

The output is as follows:

['Struct ',' _ all _ ',' _ builtins _ ',' _ cached _ ',' _ doc __', '_ file _', '_ loader _', '_ name _', '_ package _', '_ spec __', '_ unlock ache', 'calcsize', 'error', 'iter _ unpackage', 'pack _ into', 'unpackage', 'unpack _ from']

['_ Builtins _', '_ cached _', '_ doc _', '_ file __', '_ loader _', '_ name _', '_ package _', '_ spec _', 'struct ']

['_ Add _', '_ class _', '_ ins INS _', '_ delattr __', '_ delitem _', '_ dir _', '_ doc _', '_ eq _', '_ format __', '_ ge _', '_ getattribute _', '_ getitem _', '_ gt _', '_ hash __', '_ iadd _', '_ imul _', '_ init _', '_ iter _', '_ le __', '_ len _', '_ lt _', '_ mul _', '_ ne _', '_ new __', '_ reduce _', '_ performance_ex _', '_ repr _', '_ reversed _', '_ rmul __', '_ setattr _', '_ setitem _', '_ sizeof _', '_ str _', '_ subclasshook __', 'append', 'clear', 'copy', 'Count', 'extend', 'index', 'insert', 'pop', 'delete', 'reverse ', 'sort ']

['_ Add _', '_ class _', '_ ins INS _', '_ delattr __', '_ dir _', '_ doc _', '_ eq _', '_ format _', '_ ge __', '_ getattribute _', '_ getitem _', '_ getnewargs _', '_ gt _', '_ hash __', '_ init _', '_ iter _', '_ le _', '_ len _', '_ lt __', '_ mod _', '_ mul _', '_ ne _', '_ new _', '_ reduce __', '_ performance_ex _', '_ repr _', '_ rmod _', '_ rmul _', '_ setattr __', '_ sizeof _', '_ str _', '_ subclasshook _', 'capitalize', 'casefold', 'center', 'Count ', 'enabled', 'enabledwith', 'pandtabs ', 'Find', 'format', 'format _ Map', 'index', 'isalnum', 'isalpha ', 'isdecimal ', 'isdigit', 'isidentifier', 'islower', 'isnumeric ', 'isprintable', 'isspace', 'istitle', 'isupload', 'join ', 'ljust ', 'lower', 'lstrip', 'maketrans ', 'partition', 'replace ', 'rfind', 'rindex', 'partition ust ', 'rpartition ', 'rsplit ', 'rdstrip', 'split', 'splits', 'startswith ', 'strip', 'swapcase', 'title', 'translate', 'uppper ', 'zfill']

['Region', 'location', 'Perimeter ']


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.