Python
Programming
Modules
Coding Tips
Function Listing

How to list all functions in a module?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

When working with a Python module, you often need to find out what functions it provides. This is especially useful when exploring an unfamiliar library or when you cannot remember the exact name of a function. Python offers several built-in tools and standard library utilities to list functions programmatically, each with different levels of filtering and detail.

Using dir() for a Quick Overview

The built-in dir() function returns a sorted list of all names defined in a module. This includes functions, classes, variables, and imported names, so it gives a broad overview rather than a filtered list of functions only.

python
1import os
2
3print(dir(os))
4# ['DirEntry', 'F_OK', 'MutableMapping', ..., 'walk', 'write']

You can filter the results to show only callable items, though this still includes classes.

python
1import os
2
3callables = [name for name in dir(os) if callable(getattr(os, name))]
4print(callables[:10])
5# ['DirEntry', '_exit', 'abort', 'access', 'chdir', 'chmod', 'chown', 'chroot', 'close', 'closerange']

dir() is useful for quick interactive exploration but does not distinguish between functions, classes, and other callables.

Using inspect.getmembers() with isfunction

The inspect module provides getmembers(), which returns all members of an object as a list of (name, value) pairs. Combined with inspect.isfunction, you can filter to only functions defined in the module.

python
1import inspect
2import my_module
3
4functions = inspect.getmembers(my_module, inspect.isfunction)
5for name, func in functions:
6    print(name)

This filters out classes, constants, and imported names that are not functions. Note that inspect.isfunction matches only pure Python functions. It does not match built-in functions written in C, such as those in the os or math modules.

Matching Built-in Functions with isbuiltin

For modules implemented in C, use inspect.isbuiltin instead of inspect.isfunction.

python
1import inspect
2import math
3
4builtins = inspect.getmembers(math, inspect.isbuiltin)
5for name, func in builtins:
6    print(name)
7# acos, acosh, asin, asinh, atan, atan2, ...

To capture both pure Python and built-in functions in a single pass, combine the two predicates.

python
1import inspect
2import some_module
3
4all_functions = inspect.getmembers(
5    some_module,
6    lambda obj: inspect.isfunction(obj) or inspect.isbuiltin(obj)
7)

Checking the all Attribute

Many modules define an __all__ list that declares their public API. This list controls what is exported by from module import * and serves as the module author's curated list of public names.

python
1import os
2
3if hasattr(os, '__all__'):
4    print(os.__all__[:10])
5else:
6    print("No __all__ defined")

Not every module defines __all__. When it is absent, from module import * falls back to exporting all names that do not start with an underscore. You can use __all__ as a first check and fall back to inspect.getmembers() if it is not present.

python
1import inspect
2import some_module
3
4if hasattr(some_module, '__all__'):
5    public_names = some_module.__all__
6else:
7    public_names = [
8        name for name, obj in inspect.getmembers(some_module)
9        if not name.startswith('_') and (inspect.isfunction(obj) or inspect.isbuiltin(obj))
10    ]
11print(public_names)

Using help() for Interactive Exploration

The built-in help() function prints comprehensive documentation for a module, including all functions with their signatures and docstrings. It is best suited for interactive use in a Python shell.

python
import json

help(json)

This outputs the full module documentation. You can also call help() on a specific function to see only its documentation.

python
import json

help(json.dumps)

help() is not practical for programmatic use because it writes to stdout rather than returning a data structure.

Filtering Functions from a Specific Module Only

When a module imports functions from other modules, inspect.getmembers() returns those imported functions too. To list only functions that are actually defined in the module, check each function's __module__ attribute.

python
1import inspect
2import my_module
3
4own_functions = [
5    name for name, func in inspect.getmembers(my_module, inspect.isfunction)
6    if func.__module__ == my_module.__name__
7]
8print(own_functions)

This is particularly useful for large modules that re-export utilities from internal submodules.

Listing Functions from Installed Packages

You can programmatically import a module by name using importlib and then inspect it. This is useful when the module name is determined at runtime.

python
1import importlib
2import inspect
3
4module_name = "collections"
5mod = importlib.import_module(module_name)
6
7functions = inspect.getmembers(mod, inspect.isfunction)
8print(f"Functions in {module_name}:")
9for name, _ in functions:
10    print(f"  {name}")

For packages with submodules, you can use pkgutil.walk_packages() to discover and inspect all submodules.

python
1import pkgutil
2import importlib
3import inspect
4
5package_name = "json"
6package = importlib.import_module(package_name)
7
8for importer, modname, ispkg in pkgutil.walk_packages(
9    path=package.__path__, prefix=package.__name__ + "."
10):
11    mod = importlib.import_module(modname)
12    funcs = inspect.getmembers(mod, inspect.isfunction)
13    if funcs:
14        print(f"\n{modname}:")
15        for name, _ in funcs:
16            print(f"  {name}")

Common Pitfalls

  • Confusing isfunction with isbuiltin: inspect.isfunction only matches pure Python functions. Built-in C functions like math.sqrt require inspect.isbuiltin, so using the wrong predicate returns an empty list for C-extension modules.
  • Including imported functions in the results: inspect.getmembers() returns all functions visible in the module namespace, including re-exports. Filter by func.__module__ == module.__name__ to get only locally defined functions.
  • Assuming dir() returns only functions: dir() returns all attribute names including classes, constants, and submodules. Always apply a filter such as callable() or inspect.isfunction if you only want functions.
  • Relying on all for completeness: Not all modules define __all__, and some modules define it but do not keep it updated. Use it as a hint for the public API but verify with inspect for thorough discovery.
  • Using help() in scripts: help() prints to stdout and cannot be easily captured or parsed. Use inspect.getdoc() or inspect.signature() for programmatic access to documentation and signatures.

Summary

  • Use dir(module) for a quick look at all names in a module, then filter with callable() for a rough function list.
  • Use inspect.getmembers(module, inspect.isfunction) for accurate filtering of pure Python functions.
  • Use inspect.isbuiltin for C-implemented functions in modules like math and os.
  • Check module.__all__ for the author-defined public API, but do not rely on it exclusively.
  • Filter by func.__module__ to exclude re-exported functions and show only those defined in the target module.

Course illustration
Course illustration