python入門必會的助手函式:dir()函式
今天我們來看一個非常重要的函式:dir()
中文說明:不帶引數時,返回當前範圍內的變數、方法和定義的型別列表;帶引數時,返回引數的屬性、方法列表。如果引數包含方法__dir__(),該方法將被呼叫。如果引數不包含__dir__(),該方法將最大限度地收集引數資訊。
引數object: 物件、變數、型別。
版本:該函式在python各個版本中都有,但是每個版本中顯示的屬性細節有所不同。使用時注意區別。
英文說明:
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. 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 default dir() mechanism 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.
例如
>>>import struct >>>dir() # show the names in the module namespace ['__builtins__','__doc__','__name__','struct'] >>>dir(struct) # show the names in the struct module ['Struct','__builtins__','__doc__','__file__','__name__', '__package__','_clearcache','calcsize','error','pack','pack_into', 'unpack','unpack_from'] >>>class Shape(object): def __dir__(self): return ['area','perimeter','location'] >>> s= Shape() >>>dir(s)
['area', 'perimeter', 'location'] Note 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() ['__builtins__','__doc__','__name__','__package__'] >>>import struct >>>dir() ['__builtins__','__doc__','__name__','__package__','struct'] >>>dir(struct) ['Struct','__builtins__','__doc__','__file__','__name__','__package__','_clearcache','calcsize','error','pack', 'pack_into','unpack','unpack_from'] >>>class Person(object): ... def __dir__(self): ... return ["name","age","country"] ... >>>dir(Person) ['__class__','__delattr__','__dict__','__dir__','__doc__','__format__','__getattribute__','__hash__','__init__', '__module__','__new__','__reduce__','__reduce_ex__','__repr__','__setattr__','__sizeof__','__str__','__subclasshook__', '__weakref__'] >>> tom= Person() >>>dir(tom) ['age','country','name']
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/2524/viewspace-2837065/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- python函式每日一講 - dir()函式Python函式
- python函式每日一講 - dir()Python函式
- Python入門-函式Python函式
- python必會的函式或者操作Python函式
- python入門:range函式Python函式
- 常用助手函式函式
- python-函式入門(一)Python函式
- Python匿名函式快速入門Python函式
- 尤拉函式入門函式
- 生成函式入門函式
- 前端入門——函式前端函式
- 11函式入門函式
- Python入門之函式呼叫(二)Python函式
- 前端 help 助手函式前端函式
- Hyperf 常用助手函式函式
- Python函式每日一講 - frozenset集合函式入門及例項Python函式
- [譯] 函式式 JavaScript 快速入門函式JavaScript
- Python開發的入門教程(六)-函式Python函式
- 學習javaScript必知必會(1)~js介紹、函式、匿名函式、自呼叫函式、不定長引數JavaScriptJS函式
- PHP入門之函式PHP函式
- 集合與函式入門函式
- python3.7 函式基礎入門Python函式
- 5-快速入門Python函式使用Python函式
- 【廖雪峰python入門筆記】函式Python筆記函式
- python中id()函式、zip()函式、map()函式、lamda函式Python函式
- C++入門記-建構函式和解構函式C++函式
- PHP中的list()函式入門PHP函式
- Python 擴充之特殊函式(lambda 函式,map 函式,filter 函式,reduce 函式)Python函式Filter
- JavaScript入門-函式function(二)JavaScript函式Function
- 3.JavaScript函式入門JavaScript函式
- day07-函式入門函式
- 函式式DDD架構入門 - SCOTT WLASCHIN函式架構
- Laravel 新增自定義助手函式Laravel函式
- Lambda表示式入門--函數語言程式設計與函式式介面函數程式設計函式
- python內建函式-eval()函式與exec()函式的區別Python函式
- python函式每日一講 - format函式字串格式化入門Python函式ORM字串格式化
- 《MySQL 入門教程》第 16 篇 MySQL 常用函式之日期函式MySql函式
- Python函式與lambda 表示式(匿名函式)Python函式