Python 内置函数 dir():获取对象属性和方法
Python 的内置函数 dir() 可以返回一个对象的属性和方法列表。它可以用于任何 Python 对象,例如模块、类、函数、字符串等。
调用 dir() 时,如果不传入参数,则返回当前作用域内的所有名称。如果传入参数,则返回该对象的属性和方法列表。
示例:
import math
# 返回 math 模块的属性和方法列表
print(dir(math))
# 返回当前作用域内的所有名称
print(dir())
输出:
['__doc__', '__loader__', '__name__', '__package__', '__spec__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'comb', 'copysign', 'cos', 'cosh', 'degrees', 'dist', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'gcd', 'hypot', 'inf', 'isclose', 'isfinite', 'isinf', 'isnan', 'isqrt', 'lcm', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'log2', 'modf', 'nan', 'perm', 'pi', 'pow', 'prod', 'radians', 'remainder', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'tau', 'trunc']
['__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__', 'math']
原文地址: https://www.cveoy.top/t/topic/nXRV 著作权归作者所有。请勿转载和采集!