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 lists public and non-public functions ?

# Importing math module
import math

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

The output displays the most relevant functions 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__ attribute gives a list of all public functions which are imported when using from module 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 access this attribute ?

# Importing re module
import re

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

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 members inside a module, and isfunction filters to show only the functions. Using inspect allows for all functions in a module to be displayed more precisely than dir() ?

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

# Importing math module
import math

# Getting only functions from math module
functions = getmembers(math, isfunction)
print(functions)

The output lists only the actual functions from the math module ?

[]

Getting Functions from a Custom Module

Since built-in modules like math are implemented in C, inspect.isfunction() won't detect them. Let's create a custom module example ?

# Creating a sample module inline
def add(a, b):
    return a + b

def subtract(a, b):
    return a - b

def multiply(a, b):
    return a * b

# Using inspect to get functions
from inspect import getmembers, isfunction
import sys

# Get current module
current_module = sys.modules[__name__]
functions = getmembers(current_module, isfunction)

print("Functions in current module:")
for name, func in functions:
    if not name.startswith('_'):  # Skip private functions
        print(f"- {name}")
Functions in current module:
- add
- subtract
- multiply

Comparison

Method Returns Best For
dir() All attributes (functions, variables, classes) General exploration of module contents
__all__ Public API functions only Understanding module's intended interface
inspect.getmembers() Filtered by type (functions only) Precise filtering by object type

Conclusion

Use dir() for quick exploration, __all__ for public API functions, and inspect.getmembers() for precise function filtering. Each method serves different purposes depending on your specific needs.

Updated on: 2026-03-24T17:02:11+05:30

52K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements