Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can access the list of all the build in functions for an module in python

Tags:

python

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"

like image 464
JAZs Avatar asked Jan 21 '26 00:01

JAZs


2 Answers

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

like image 186
Alexander Zhukov Avatar answered Jan 22 '26 14:01

Alexander Zhukov


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>
like image 38
Tim Pietzcker Avatar answered Jan 22 '26 13:01

Tim Pietzcker



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!