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"

3 Answers 3

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?.

Sign up to request clarification or add additional context in comments.

Comments

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>

Comments

2

For only builtin functions:

from inspect import getmembers, isfunction

functions_list = [o for o in getmembers(my_module, isfunction)]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.