Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Inspect live objects in Python
The inspect module in Python provides powerful functions to examine live objects such as modules, classes, methods, functions, and code objects. These functions perform type checking, retrieve source code, inspect classes and functions, and examine the interpreter stack.
Key Functions
getmembers() − Returns all members of an object in a list of name-value pairs sorted by name. Optional predicate parameter filters results.
getmodulename() − Returns the module name from a file path, excluding enclosing package names.
Example Module
Let's create a sample module to demonstrate inspect functionality ?
# inspect_example.py
'''This is module docstring'''
def hello():
'''hello docstring'''
print('Hello world')
return
# Class definitions
class parent:
'''parent docstring'''
def __init__(self):
self.var = 'hello'
def hello(self):
print(self.var)
class child(parent):
def hello(self):
'''hello function overridden'''
super().hello()
print("How are you?")
Using getmembers()
Get all non-private members of the module ?
import inspect
import inspect_example
for name, obj in inspect.getmembers(inspect_example):
if not name.startswith('__'):
print(name, obj)
child <class 'inspect_example.child'> hello <function hello at 0x...> parent <class 'inspect_example.parent'>
Predicates
Predicates are logical conditions that filter objects based on their type. The inspect module provides several built-in predicates ?
| Predicate | Description |
|---|---|
ismodule() |
Return true if the object is a module |
isclass() |
Return true if the object is a class |
ismethod() |
Return true if the object is a bound method |
isfunction() |
Return true if the object is a Python function |
isgenerator() |
Return true if the object is a generator |
iscode() |
Return true if the object is a code object |
isbuiltin() |
Return true if the object is a built-in function |
Example: Getting Only Classes
import inspect
import inspect_example
for name, obj in inspect.getmembers(inspect_example, inspect.isclass):
print(name, obj)
child <class 'inspect_example.child'> parent <class 'inspect_example.parent'>
Retrieving Documentation
Use getdoc() to retrieve docstrings from modules, classes, or functions ?
import inspect
import inspect_example
print("Module docstring:", inspect.getdoc(inspect_example))
print("Class docstring:", inspect.getdoc(inspect_example.parent))
print("Function docstring:", inspect.getdoc(inspect_example.hello))
Module docstring: This is module docstring Class docstring: parent docstring Function docstring: hello docstring
Getting Source Code
Use getsource() to retrieve the source code of functions and classes ?
import inspect import inspect_example print(inspect.getsource(inspect_example.hello))
def hello():
'''hello docstring'''
print('Hello world')
return
Function Signatures
Examine function signatures using signature() ?
import inspect
import inspect_example
sig = inspect.signature(inspect_example.parent.hello)
print("Signature:", sig)
Signature: (self)
Command Line Interface
The inspect module also provides command-line functionality to examine modules and functions directly from the terminal.
To inspect a module ?
python -m inspect -d inspect_example
To view source code of a specific function ?
python -m inspect inspect_example:hello
Conclusion
The inspect module is essential for debugging, documentation generation, and dynamic code analysis. Use getmembers() with predicates to filter objects, getdoc() for documentation, and getsource() for source code examination.
