描述:
返回结果列表按字母顺序排序,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()函数就学到这里。