获取python标准库函数名称列表

时间:2019-04-22 05:28:17

标签: python

我可以使用dir(__builtins__)获取python内置函数的列表。 我想知道是否有一种方法可以获取python标准库的函数名称列表,包括appendMath.exp等。

2 个答案:

答案 0 :(得分:1)

append是类list的方法,因此您可以从dir(list)获取它。

类似地,math.exp是模块math的自由函数,并且dir(math)包含exp

鉴于您只需要方法/函数,并假设要避免使用非公共属性,则可以执行以下操作:

import math

def is_public(name):
    return not (name.startswith('__') and name.endswith('__'))

def get_functions(source):
    return [name for name in dir(source) if callable(getattr(source, name)) and is_public(name)]

print(f'Methods of class list: {get_functions(list)}', end='\n\n')
print(f'Functions of module math: {get_functions(math)}')

输出:

Methods of class list: ['append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']

Functions of module math: ['acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'gcd', 'hypot', 'isclose', 'isfinite', 'isinf', 'isnan', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'log2', 'modf', 'pow', 'radians', 'remainder', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'trunc']

如果确实要使用 all 个属性,则可以删除is_public条件。

答案 1 :(得分:0)

标准对象list的所有功能可以通过以下方式获得:

dir(list)

({dir(__builtins__)获取所有对象的列表)

要获取所有导入模块的列表:

help('modules') 
相关问题