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.
You can filter the results to show only callable items, though this still includes classes.
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.
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.
To capture both pure Python and built-in functions in a single pass, combine the two predicates.
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.
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.
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.
This outputs the full module documentation. You can also call help() on a specific function to see only its documentation.
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.
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.
For packages with submodules, you can use pkgutil.walk_packages() to discover and inspect all submodules.
Common Pitfalls
- Confusing isfunction with isbuiltin:
inspect.isfunctiononly matches pure Python functions. Built-in C functions likemath.sqrtrequireinspect.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 byfunc.__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 ascallable()orinspect.isfunctionif 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 withinspectfor thorough discovery. - Using help() in scripts:
help()prints to stdout and cannot be easily captured or parsed. Useinspect.getdoc()orinspect.signature()for programmatic access to documentation and signatures.
Summary
- Use
dir(module)for a quick look at all names in a module, then filter withcallable()for a rough function list. - Use
inspect.getmembers(module, inspect.isfunction)for accurate filtering of pure Python functions. - Use
inspect.isbuiltinfor C-implemented functions in modules likemathandos. - 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.

