How to list all functions in a Python module?


In this article we will discuss how to list all the functions in a Python module.

A Python module contains multiple different functions that allow for extensive code reusability making complex code simple. It also enhances portability of python program by changing platform dependent code into platform independent APIs

Python standard library consists of modules written in C that provide access to system functionality and modules written in python that provide general solutions for everyday problems making the life of programmers easy as it prevents writing of long code for simple problems

Using dir() to get functions in a module

Python's dir() function is used to display the names of all the functions and variables present in a module. The function produces the most relevant result rather than the complete result as it list public and non-public functions.

Example

The below code gives an example of using dir() to get the relevant functions of math module.

# Importing math module
import math as mt

# Printing all the functions in math module using dir
print(dir(mt))

Output

The output displays the most relevant function in the math module.

['__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', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'log2', 'modf', 'nan', 'perm', 'pi', 'pow', 'prod', 'radians', 'remainder', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'tau', 'trunc']

Using __all__ to get functions in a module

The __all__ gives a list of all public functions which are imported when using import *. It gives all the functions that do not start with an underscore (_) before them. Modules that do not define __all__ will throw an AttributeError if a user tries to get the functions within that module.

Example

The code below gives a demo of using __all__ to display different functions in re module.

# Importing re module
import re

# Printing different functions in re module
print(re.__all__)

Output

The output gives the different functions present in re module.

['match', 'fullmatch', 'search', 'sub', 'subn', 'split', 'findall', 'finditer', 'compile', 'purge', 'template', 'escape', 'error', 'Pattern', 'Match', 'A', 'I', 'L', 'M', 'S', 'X', 'U', 'ASCII', 'IGNORECASE', 'LOCALE', 'MULTILINE', 'DOTALL', 'VERBOSE', 'UNICODE']

Using inspect to get functions in a module

Python inspect library can be used to get the functions under any module. The getmembers() function gets all the functions and variables inside a module and then isfunction filters to show only the functions. Using inspect allows for all functions in a module to be displayed unlike dir().

Example

The code given below shows use of getmembers() and isfunction in inspect library to get functions of a module.

# Importing getmembers and isfunction from inspect
from inspect import getmembers, isfunction

# Importing math module
import math as mt

# Printing all the functions in math module
print(getmembers(mt), isfunction)

Output

The output lists all the functions present in the math module.

[('__doc__', 'This module provides access to the mathematical functions\ndefined by the C standard.'), ('__loader__', <class '_frozen_importlib.BuiltinImporter'>), ('__name__', 'math'), ('__package__', ''), ('__spec__', ModuleSpec(name='math', loader=<class '_frozen_importlib.BuiltinImporter'>, origin='built-in')), ('acos', <built-in function acos>), ('acosh', <built-in function acosh>), ('asin', <built-in function asin>), ('asinh', <built-in function asinh>), ('atan', <built-in function atan>), ('atan2', <built-in function atan2>), ('atanh', <built-in function atanh>), ('ceil', <built-in function ceil>), ('comb', <built-in function comb>), ('copysign', <built-in function copysign>), ('cos', <built-in function cos>), ('cosh', <built-in function cosh>), ('degrees', <built-in function degrees>), ('dist', <built-in function dist>), ('e', 2.718281828459045), ('erf', <built-in function erf>), ('erfc', <built-in function erfc>), ('exp', <built-in function exp>), ('expm1', <built-in function expm1>), ('fabs', <built-in function fabs>), ('factorial', <built-in function factorial>), ('floor', <built-in function floor>), ('fmod', <built-in function fmod>), ('frexp', <built-in function frexp>), ('fsum', <built-in function fsum>), ('gamma', <built-in function gamma>), ('gcd', <built-in function gcd>), ('hypot', <built-in function hypot>), ('inf', inf), ('isclose', <built-in function isclose>), ('isfinite', <built-in function isfinite>), ('isinf', <built-in function isinf>), ('isnan', <built-in function isnan>), ('isqrt', <built-in function isqrt>), ('ldexp', <built-in function ldexp>), ('lgamma', <built-in function lgamma>), ('log', <built-in function log>), ('log10', <built-in function log10>), ('log1p', <built-in function log1p>), ('log2', <built-in function log2>), ('modf', <built-in function modf>), ('nan', nan), ('perm', <built-in function perm>), ('pi', 3.141592653589793), ('pow', <builtin function pow>), ('prod', <built-in function prod>), ('radians', <built-in function radians>), ('remainder', <built-in function remainder>), ('sin', <built-in function sin>), ('sinh', <built-in function sinh>), ('sqrt', <builtin function sqrt>), ('tan', <built-in function tan>), ('tanh', <built-in function tanh>), ('tau', 6.283185307179586), ('trunc', <built-in function trunc>)] <function isfunction at 0x7f6c72305940>

Updated on: 26-Aug-2023

31K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements