描述:
傳回結果清單按字母順序排序,dir()函數不帶參數時,傳回目前範圍内的變量、方法和定義的類型清單;帶參數時,傳回參數的屬性、方法清單。如果參數包含方法__dir__(),該方法将被調用。如果參數不包含__dir__(),該方法将最大限度地收集參數資訊
文法:
dir([object])
參數介紹:
object --- 對象、變量、類型
傳回值:
傳回子產品的屬性清單
下面例子展示dir()函數使用方法
import struct
print(dir()) # show the names in the module namespace ,output
print(dir(struct)) # show the names in the struct module ,output
class Shape:
def __dir__(self):
return ['area', 'perimeter', 'location']
s = Shape()
print(dir(s))
輸出
['__annotations__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'struct']
['Struct', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', '_clearcache', 'calcsize', 'error', 'iter_unpack', 'pack', 'pack_into', 'unpack', 'unpack_from']
['area', 'location', 'perimeter']
本期dir()函數就學到這裡。