2

If i have one module called "math" that can be called as import "math", then how can i get the list of all the build in functions associated with "math"

JAZs
  • 29
  • 7

3 Answers3

4

There is a dir function, which lists all (well, pretty much) attributes of the object. But to filter only functions isn't a problem:

 >>>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']

You might find the Guide to Python introspection to be a useful resource, as well as this question: how to detect whether a python variable is a function?.

Community
  • 1
  • 1
Alexander Zhukov
  • 4,357
  • 1
  • 20
  • 31
3

That's where help() comes in handy (if you prefer a human-readable format):

>>> 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>
Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561
2

For only builtin functions:

from inspect import getmembers, isfunction

functions_list = [o for o in getmembers(my_module, isfunction)]
user278064
  • 9,982
  • 1
  • 33
  • 46