2

インポート「数学」として呼び出すことができる「数学」と呼ばれるモジュールが 1 つある場合、「数学」に関連付けられているすべての組み込み関数のリストを取得するにはどうすればよいですか

4

3 に答える 3

4

オブジェクトのすべての (ほとんどの) 属性を一覧表示するdir関数があります。ただし、関数のみをフィルタリングすることは問題ではありません。

 >>>import math
 >>>dir(math)
 ['__doc__', '__name__', '__package__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'hypot', 'isinf', 'isnan', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'modf', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'trunc']
 >>>
 >>>[f for f in dir(math) if hasattr(getattr(math, f), '__call__')] # filter on functions
 ['acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'hypot', 'isinf', 'isnan', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'modf', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'trunc']

Guide to Python introspectionが役立つリソースであることに気付くかもしれません。また、この質問: how to detect whether a python variable is a function? .

于 2013-10-22T09:16:32.807 に答える
3

それhelp()が便利なところです(人間が読める形式を好む場合):

>>> import math
>>> help(math)
Help on built-in module math:

NAME
    math

<snip> 

FUNCTIONS
    acos(...)
        acos(x)

        Return the arc cosine (measured in radians) of x.

    acosh(...)
        acosh(x)

        Return the hyperbolic arc cosine (measured in radians) of x.

    asin(...)
<snip>
于 2013-10-22T09:16:54.800 に答える