Built-in Functions - 內置函數 - dir()

Built-in Functions - 內置函數 - dir()

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

The Python interpreter has a number of functions and types built into it that are always available.
Python 解釋器內置了很多函數和類型,您可以在任何時候使用它們。

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 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 way dir() reports their attributes.
如果對象有一個名爲 __dir__() 的方法,那麼該方法將被調用,並且必須返回一個屬性列表。這允許實現自定義 __getattr__()__getattribute__() 函數的對象能夠自定義 dir() 來報告它們的屬性。

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__().
如果對象不提供 __dir__(),這個函數會嘗試從對象已定義的 __dict__ 屬性和類型對象收集信息。結果列表並不總是完整的,如果對象有自定義 __getattr__(),那結果可能不準確。

The default dir() mechanism behaves differently with different types of objects, as it attempts to produce the most relevant, rather than complete, information:
默認的 dir() 機制對不同類型的對象行爲不同,它會試圖返回最相關而不是最全的信息:

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

  • 如果對象是模塊對象,則列表包含模塊的屬性名稱。

  • 如果對象是類型或類對象,則列表包含它們的屬性名稱,並且遞歸查找所有基類的屬性。

  • 否則,列表包含對象的屬性名稱,它的類屬性名稱,並且遞歸查找它的類的所有基類的屬性。

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

The resulting list is sorted alphabetically. For example:
返回的列表按字母表排序。例如:

>>> 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 primarily as a convenience for use at an interactive prompt, it tries to supply an interesting set of names more than it tries to supply a rigorously or consistently defined set of names, and its detailed behavior may change across releases. For example, metaclass attributes are not in the result list when the argument is a class.
因爲 dir() 主要是爲了便於在交互式時使用,所以它會試圖返回人們感興趣的名字集合,而不是試圖保證結果的嚴格性或一致性,它具體的行爲也可能在不同版本之間改變。例如,當實參是一個類時,metaclass 的屬性不包含在結果列表中。

查看當前模塊的屬性列表 - 查看列表的方法

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>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章