When working with large Python scripts, you might want to list all functions defined in the current file — especially for debugging, documentation, or automation purposes.
In this article, you’ll learn multiple ways to list all functions defined in the current script using modules like inspect, globals(), and ast.
Method 1: Using inspect.getmembers() (Best & Recommended)
import inspect import sys
# Get current module
current_module = sys.modules[name]
# List all functions defined in this file
functions = [name for name, obj in inspect.getmembers(current_module, inspect.isfunction)]
print(functions)
Output:
['greet', 'add', 'subtract']
This method works even when functions are imported from other files.
Method 2: Using globals() and callable()
Python’s built-in globals() returns a dictionary of all symbols defined in the current scope. You can use it to check for all functions dynamically.
def greet(): pass def add(a, b): return a + b def subtract(a, b): return a - b
# List all user-defined functions
functions = [name for name, obj in globals().items() if callable(obj)]
print(functions)
Output:
['greet', 'add', 'subtract']
Method 3: Using inspect.isfunction() with globals()
You can make the filtering more strict by combining inspect.isfunction() to ensure you only list real function definitions.
import inspect
def greet(): pass
def add(a, b): return a + b
def subtract(a, b): return a - b
functions = [name for name, obj in globals().items()
if inspect.isfunction(obj)]
print(functions)
Output:
['greet', 'add', 'subtract']
This method excludes classes or built-ins accidentally included by globals().
Method 4: Using ast (Abstract Syntax Tree)
If you want to analyze the source code file itself, without importing or executing it, use Python’s ast module.
import ast
# Parse the current file
with open(file, "r") as f:
tree = ast.parse(f.read())
# Extract function names
functions = [node.name for node in ast.walk(tree)
if isinstance(node, ast.FunctionDef)]
print(functions)
Output:
['greet', 'add', 'subtract']
Filter Only User-Defined Functions (Exclude Imports)
You can check which module each function belongs to and only include ones from the current file.
import inspect import sys
current_module = sys.modules[name]
# Filter only functions defined in this file
functions = [
name for name, obj in inspect.getmembers(current_module, inspect.isfunction)
if inspect.getmodule(obj).name == name
]
print(functions)
Output:
['greet', 'add', 'subtract']
FAQs — Python: Get List of All Functions in Current File
How to get a list of all functions defined in the current Python file?
Use the inspect module to retrieve all user-defined functions:
import inspect, sys
functions = [name for name, obj in inspect.getmembers(sys.modules[__name__], inspect.isfunction)]
print(functions)
This lists all function names defined in the current file.
How does inspect.getmembers() work for listing functions in a Python module or file?
inspect.getmembers() scans a module’s namespace and filters objects based on a condition, such as inspect.isfunction for functions.
inspect.getmembers(sys.modules[__name__], inspect.isfunction)
It returns tuples of function name and object pairs.
How to list only user-defined functions (excluding imported or built-in) in the current Python file?
You can filter by the module name to ensure only functions from the current script are included:
[f for f, obj in inspect.getmembers(sys.modules[__name__], inspect.isfunction)
if obj.__module__ == '__main__']
This excludes functions imported from other modules.
Can I list both functions and classes defined in the current Python script?
Yes — you can call inspect.getmembers() twice, once for inspect.isfunction and once for inspect.isclass:
import inspect, sys
funcs = [f for f, _ in inspect.getmembers(sys.modules[__name__], inspect.isfunction)]
classes = , inspect.isclass)]
print(funcs, classes)
How to print all function names in the current Python file dynamically?
Iterate over the result of inspect.getmembers() and print each function name:
for name, func in inspect.getmembers(sys.modules[__name__], inspect.isfunction):
print(name)
How to get both function names and their docstrings from the current Python file?
You can access __doc__ or use inspect.getdoc() for each function:
for name, func in inspect.getmembers(sys.modules[__name__], inspect.isfunction):
print(name, ":", inspect.getdoc(func))
Is there a way to get all function names using dir() instead of inspect in Python?
Yes, but dir() lists everything — you’ll need to filter manually:
import types
[name for name, obj in globals().items() if isinstance(obj, types.FunctionType)]
This approach uses globals() and checks for function objects directly.
Which is better for listing functions in Python — dir() or inspect?
- dir(): simple, fast, returns names only.
- inspect: returns function objects, signatures, and docstrings — more powerful for introspection.