PythonPandas.com

How to list all Functions in a Python module



While exploring a new Python module, we often want to see all available functions. Mostly to understand what it offers without opening the source code manually.

In this article, we’ll see different ways to list all functions in a Python module, using built-in libraries like dir(), inspect, and even command-line methods, with code examples.


There are several ways to list functions in a Python module. dir() gives all attributes, while inspect.getmembers() can return only functions. For robust introspection and access to function objects (docstrings, signatures), use inspect.

Method 1 — dir() (quick)

dir() lists all attribute names (functions, classes, variables, dunder names). Use it when you want a fast overview.

import math

# All attribute names in the module
print(dir(math))

Note: you’ll need to filter results because dir() lists everything.

Method 2 — inspect.getmembers() (recommended)

The inspect module is the most reliable way to get function objects from a module. It allows filtering and gives you real function objects (so you can access .__doc__, signatures, etc.).

import inspect
import math

# Returns list of (name, function_object) pairs for functions
functions = inspect.getmembers(math, inspect.isfunction)

# Print only names
for name, _ in functions:
    print(name)

You can also inspect docstrings or signatures:

for name, func in functions:
    print(name, "-", func.__doc__)

Method 3 — types.FunctionType

Another method is to filter the module’s __dict__ by types.FunctionType. Works well for user-defined functions.

import types
import math

functions = [name for name, obj in math.__dict__.items() if isinstance(obj, types.FunctionType)]
print(functions)

Method 4 — Listing functions from your own module

If you have a module file like my_module.py, you can import it and use inspect.getmembers() to list functions:

# my_module.py
def add(a, b): return a + b
def sub(a, b): return a - b
def greet(): print("Hello!")
PI = 3.1416
# list functions from my_module
import inspect
import my_module

functions = [name for name, obj in inspect.getmembers(my_module, inspect.isfunction)]
print(functions)  # ['add', 'greet', 'sub']

Method 5 — Command line (pydoc)

Use pydoc to view module documentation including function signatures:

python -m pydoc math

This prints the module documentation to stdout (useful for quick discovery in the terminal).

Filtering: only user-defined functions

To exclude imported or builtin functions, check the function’s defining module:

import inspect
import my_module

for name, func in inspect.getmembers(my_module, inspect.isfunction):
    # ensure the function is defined in my_module (not imported)
    if inspect.getmodule(func).__name__ == my_module.__name__:
        print(name)

This prints only functions whose __module__ equals my_module, i.e., functions defined in that module.

FAQs — List All Functions in a Python Module

Q: How do I list only user-defined functions in a module?

A: Use inspect.getmembers(module, inspect.isfunction) and filter by inspect.getmodule(func).__name__ (or func.__module__) to match your module name.

Q: What’s the difference between dir() and inspect?

A: dir() lists all attribute names (functions, classes, variables, dunder methods). inspect lets you filter by object type (e.g., inspect.isfunction, inspect.isclass) and returns function objects.

Q: Can I also list classes the same way?

A: Yes — use inspect.getmembers(module, inspect.isclass) to get all classes defined in the module.

Q: How can I print docstrings or signatures for all functions?

A: Use inspect.getmembers(..., inspect.isfunction) and then print func.__doc__ or inspect.signature(func) for each function:

import inspect, my_module
for name, func in inspect.getmembers(my_module, inspect.isfunction):
    print(name, inspect.signature(func))
    print(func.__doc__)


Related Post