Built-in Functions - built-in functions - dir ()

Built-in Functions - built-in functions - dir ()

https://docs.python.org/3/library/functions.html
https://docs.python.org/zh-cn/3/library/functions.html

A Number The Python Interpreter has at The Functions and types of Built INTO IT that are Always the Available.
Python interpreter built a lot of functions and types that you can use them at any time.

1. 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 there is no argument, the current list of local scope name is returned. If there are arguments, it tries to return a list of valid property of the object.

Method A The Object has IF the named __dir__(), and the this Method Will MUST BE Called The return List of Attributes. This android.permission A Custom Objects that Implement __getattr__()or __getattribute__()function to Customize The Way dir()Reports Their Attributes.
If there is an object called a __dir__()method, the method It will be invoked, and must return a list of attributes. This allows for custom __getattr__()or __getattribute__()function of the object can be custom dir()reporting their attributes.

IF at The Object does not the Provide __dir__(), at The function tries the ITS Best to Gather Information from at The Object's __dict__attribute, IF defined, and from the ITS of the type Object. At The the Resulting List IS not Necessarily Complete, and May BE Inaccurate the when at The Object has A Custom __getattr__().
If the object It does not provide __dir__()this function attempts from an object defined __dict__attributes and types of objects to collect information. The results list is not always complete, if the object has a custom __getattr__(), that the results may be inaccurate.

At The default dir()mechanism behaves Differently with Different types of Objects, AS IT attempts unsuccessful attempts to Produce at The MOST Relevant, The Rather Within last Complete, Information:
default dir()mechanism for object behavior of different types of different, it will attempt to return the most relevant, rather than the most 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.

  • If the object is a module object, the list contains the attribute name of the module.

  • If the object is an object type or class, the list contains the names of their properties, and the properties of recursive search of all the base class.

  • Otherwise, a list containing the name of the object, its class attribute names, and recursive search of its properties base class for all classes.

alphabetically [,ælfe'bɛtɪkli]:adv. 照字母顺序排列地

The resulting list is sorted alphabetically For example :.
Returned list sorted alphabetically. E.g:

>>> import struct
>>> dir()   # show the names in the module namespace  # doctest: +SKIP
['__builtins__', '__name__', 'struct']
>>> dir(struct)   # show the names in the struct module # doctest: +SKIP
['Struct', '__all__', '__builtins__', '__cached__', '__doc__', '__file__',
 '__initializing__', '__loader__', '__name__', '__package__',
 '_clearcache', 'calcsize', 'error', 'pack', 'pack_into',
 'unpack', 'unpack_from']
>>> class Shape:
...     def __dir__(self):
...         return ['area', 'perimeter', 'location']
>>> s = Shape()
>>> dir(s)
['area', 'location', 'perimeter']

Because dir()IS Supplied the Primarily AS A Convenience for use AT AN Interactive prompt, IT tries to Supply's AN interesting the SET of names More Within last IT tries to Supply's A rigorously or Consistently defined the SET of names, and the ITS the Detailed behavior May Change across Releases. The For Example, metaclass attributes are not in the result list when the argument is a class.
because dir()primarily for ease of use in an interactive, so it will try to return the names of people interested in the collection, rather than trying to ensure strict or consistent results, it is specific behaviors may change between different versions. For example, when the argument is a class, the properties of the metaclass that is not included in the results list.

View the current list of attributes module - to view the list of methods

Microsoft Windows [版本 6.1.7601]
版权所有 (c) 2009 Microsoft Corporation。保留所有权利。

C:\Users\foreverstrong>python
Python 3.6.0 (v3.6.0:41df79263a11, Dec 23 2016, 08:06:12) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> dir()
['__annotations__', '__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__']
>>>
>>> dir([])
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__init_subclass__', '__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']
>>>
>>> exit()

C:\Users\foreverstrong>

Guess you like

Origin blog.csdn.net/chengyq116/article/details/93538967