Python 中的 dir() 函数

pythonserver side programmingprogramming更新于 2025/4/20 13:32:17

内置函数 dir() 返回一个排序的字符串列表,其中包含模块定义的名称。

该列表包含模块中定义的所有模块、变量和函数的名称。以下是一个简单的示例 −

示例

#!/usr/bin/python
# 导入内置模块 math
import math
content = dir(math)
print content

输出

执行上述代码时,会产生以下结果 −

['__doc__', '__file__', '__name__', 'acos', 'asin', 'atan',
'atan2', 'ceil', 'cos', 'cosh', 'degrees', 'e', 'exp',
'fabs', 'floor', 'fmod', 'frexp', 'hypot', 'ldexp', 'log',
'log10', 'modf', 'pi', 'pow', 'radians', 'sin', 'sinh',
'sqrt', 'tan', 'tanh']

此处,特殊字符串变量 __name__ 是模块的名称,__file__ 是加载模块的文件名。


相关文章


有用资源