The inspect module is a useful tool for introspecting live objects, such as modules, classes, functions, and methods. It provides several functions to help examine the structure and metadata of Python code dynamically.
This is a standard library module and you don't need to perform any extra installations, you just need to import it in your program.
The inspect module can be very useful, for example, you can use it to:
-
Retrieve the source code of functions and classes.
-
Extract information about callable objects (arguments, defaults, etc.).
-
Inspect class hierarchies and method resolution order.
-
e.t.c
In this article, we will explore the key functions of the inspect module, along with practical examples to demonstrate their usage.
Retrieving Information About Objects
inspect.ismodule()
This function checks whether the given object is a module.
inspect.isclass()
Checks whether the given object is a class.
inspect.isfunction()
Checks whether the given object is a user-defined function, regular or lambda.
inspect.ismethod()
Checks if the given object is a bound method.
inspect.isbuiltin()
Checks if the given object is a builtin function.
inspect.iskeyword()
Checks if the given string is a valid keyword in Python.
Extracting Source Code
You can use the inspect module to extract the source code of objects.
inspect.getsource()
Returns the source code of a function, class, or method as a string.
Note: This Works only if the source code is available (e.g., not for built-in functions).
inspect.getlinesource()
Returns the source code split into lines along with the starting line number.
Inspecting Function and Method Signatures
inspect.signature()
Provides a Signature object containing parameter details (name, kind, default values).
Class and inheritance Inspection
inspect.getmro()
Returns a tuple of classes in the method resolution order (MRO).
inspect.isabstract()
Determines if a class or method is abstract (part of abc module).
Working with Frames and Call Stacks
inspect.currentframe()
Retrieves the current frame object for debugging purposes.
inspect.stack()
Returns a list of frame records for the current call stack.
Analyzing objects dynamically
inspect.getmembers()
Returns all attributes and methods present in an object.
Conclusion
The inspect module can be used to introspect Python objects dynamically. You may need this while debugging, generating documentation, or performing runtime analysis, this module provides powerful utilities to examine code structure, extract metadata, and manipulate objects programmatically.
By mastering the inspect module, you can write more flexible applications that adapt to different runtime conditions.
We only looked at a handful of the methods available in the inspect module. You can, explore further on other inspect methods. You can use the builtin dir() function to list all the available methods. In the following example, we use the filter() function to filter out the some of the unnecessary methods and attributes.