List All Functions from a Python Module

To list all the functions from a Python module, you can use the dir() function. Here’s an example:

import math

print(dir(math))

Output:

['__doc__', '__loader__', '__name__', '__package__', '__spec__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'comb', 'copysign', 'cos', 'cosh', 'degrees', 'dist', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'gcd', 'hypot', 'inf', 'isclose', 'isfinite', 'isinf', 'isnan', 'isqrt', 'lcm', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'log2', 'modf', 'nan', 'perm', 'pi', 'pow', 'prod', 'radians', 'remainder', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'tau', 'trunc']

This will output a list of all the functions in the math module. You can use this technique to list functions from other modules as well.

Method 1: Using the dir() Function:

Sure, I can provide more details on how to use the dir() function to list functions from a Python module.

The dir() function in Python is used to list all the attributes (methods and variables) of an object. When used with a module as the object, it returns a list of all the functions in that module.

Here’s an example:

import my_module

functions_list = dir(my_module)
print(functions_list)

Output:

['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'function1', 'function2', 'function3']

This code imports the my_module module and then calls the dir() function on it. The list of functions in my_module is then assigned to the functions_list variable and printed to the console.

Note that the output of dir() includes all attributes of the module, including variables, classes, and functions. To filter out just the functions, you can use a list comprehension to check for the callable() attribute:

import my_module

functions_list = [func for func in dir(my_module) if callable(getattr(my_module, func))]
print(functions_list)

Output:

['function1', 'function2', 'function3']

This code uses a list comprehension to iterate over the list returned by dir(). It then checks whether each item is callable using the callable() function, and adds it to the functions_list if it is a function.

Method 2: Using Inspect Module:

Another way to list all the functions of a Python module is by using the inspect module. The inspect module provides a way to get information about live objects like classes, functions, and modules.

Here’s an example:

import inspect
import my_module

functions_list = [func[0] for func in inspect.getmembers(my_module, inspect.isfunction)]
print(functions_list)

Output:

['function1', 'function2', 'function3']

This code imports the inspect module and the my_module module. The inspect.getmembers() function is then called with the my_module object and the inspect.isfunction function as arguments. This returns a list of all the members (functions, variables, classes) of my_module. The list comprehension is then used to filter out just the functions by checking the type of each member using inspect.isfunction, and adding it to the functions_list if it is a function.

Note that the output of this method is similar to that of the previous method, but it only returns the names of the functions, not any other attributes or variables in the module.