1. 程式人生 > 實用技巧 >Python dir() 函式

Python dir() 函式

描述

dir()函式不帶引數時,返回當前範圍內的變數、方法和定義的型別列表;最暴利的擺攤小吃https://www.cgewang.com/post/13370.html

帶引數時,返回引數的屬性、方法列表。如果引數包含方法__dir__(),該方法將被呼叫。如果引數不包含__dir__(),該方法將最大限度地收集引數資訊。

語法

dir 語法:

dir([object])

引數說明:

  • object -- 物件、變數、型別。

返回值

返回模組的屬性列表。

例項

以下例項展示了 dir 的使用方法:

>>>dir() # 獲得當前模組的屬性列表 ['__builtins__', '__doc__', '__name__', '__package__', 'arr', 'myslice'] >>> dir([ ]) # 檢視列表的方法 ['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__delslice__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getslice__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__setslice__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort'] >>>