Reputation: 29
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"
Upvotes: 2
Views: 134
Reputation: 4567
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?.
Upvotes: 4
Reputation: 10180
For only builtin functions:
from inspect import getmembers, isfunction
functions_list = [o for o in getmembers(my_module, isfunction)]
Upvotes: 2
Reputation: 336478
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>
Upvotes: 3