Python built-in functions (16) -- dir, python built-in functions dir

Source: Internet
Author: User

Python built-in functions (16) -- dir, python built-in functions dir

English document:

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 will be called and must return the list of attributes. This allows objects that implement a custom__getattr__()Or__getattribute__()Function to customize the waydir()Reports their attributes.

If the object does not provide__dir__(), The function tries its best to gather information from the object's__dict__Attribute, if defined, and from its type object. The resulting list is not necessarily complete, and may be inaccurate when the object has a custom__getattr__().

The defaultdir()Mechanic behaves differently with different types of objects, as it attempts to produce the most relevant, 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 attributes of its class's base classes.

 

Note:

1. If no parameter is input, a list of variables, methods, and definitions in the current scope will be returned.

>>> Dir () ['_ builtins _', '_ doc _', '_ loader _', '_ name __', '_ package _', '_ spec _'] >>> a = 10 # define variable a >>> dir () # An additional a ['_ builtins _', '_ doc _', '_ loader _', '_ name __', '_ package _', '_ spec _', 'a']

2. When the parameter object is a module, the system returns the attribute and method list of the module.

>>> import math>>> math<module 'math' (built-in)>>>> dir(math)['__doc__', '__loader__', '__name__', '__package__', '__spec__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'gcd', 'hypot', 'inf', 'isclose', 'isfinite', 'isinf', 'isnan', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'log2', 'modf', 'nan', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'trunc']

3. When the parameter object is a class, the attribute and method list of the class and its subclass are returned.

>>> Class A: name = 'class' >>>> a = A () >>> dir (a) # name is the attribute of class, others are the default inherited object attributes and methods ['_ class _', '_ delattr _', '_ dict __', '_ dir _', '_ doc _', '_ eq _', '_ format _', '_ ge __', '_ getattribute _', '_ gt _', '_ hash _', '_ init _', '_ le __', '_ lt _', '_ module _', '_ ne _', '_ new _', '_ reduce __', '_ performance_ex _', '_ repr _', '_ setattr _', '_ sizeof _', '_ str __', '_ subclasshook _', '_ weakref _', 'name']

4. When the object is definedThe _ dir _ method returns the result of the _ dir _ method.

>>> Class B: def _ dir _ (self): return ['name', 'age'] >>> B = B () >>> dir (B) # Call the _ dir _ method ['age', 'name']

 

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.