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.

ExampleEdit & Run
import inspect

print(inspect)
Output:

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.

ExampleEdit & Run
import inspect
import math

print(inspect.ismodule(math)) #True
print(inspect.ismodule(print)) #False
Output:
<module 'inspect' from '/usr/local/lib/python3.11/inspect.py'> [Finished in 0.07332002200200805s]

inspect.isclass() 

Checks whether the given object is a class.

ExampleEdit & Run
import inspect
import math

class Example:
   pass

print( inspect.isclass(Example) ) #True
print( inspect.isclass(math) ) #False
Output:
True False [Finished in 0.07104198099841597s]

inspect.isfunction()  

Checks whether the given object is a user-defined function, regular or  lambda.

ExampleEdit & Run
import inspect

def greet():
    print('Hello')

print( inspect.isfunction(greet) ) #True
print( inspect.isfunction(print) ) #False
Output:
True False [Finished in 0.07393842300007236s]

inspect.ismethod()  

Checks if the given object is a bound method.

ExampleEdit & Run
import inspect

class Example:
    def greet():
        print('Hello')

obj = Example()

print( inspect.ismethod(obj.greet) ) #True
print( inspect.ismethod(Example.greet) ) #False (unbound method)
Output:
True False [Finished in 0.07699846499963314s]

inspect.isbuiltin() 

Checks if the given object is a builtin function.

ExampleEdit & Run
import inspect
import math

print( inspect.isbuiltin(print) ) #True
print( inspect.isbuiltin(math.sqrt) ) #True
Output:
True False [Finished in 0.07450452400007634s]

inspect.iskeyword()  

Checks if the given string is a valid keyword in Python.

ExampleEdit & Run
import inspect
import math

print( inspect.iskeyword('for') ) 
print( inspect.iskeyword('while') ) 
print( inspect.iskeyword('when') ) 
Output:
True True [Finished in 0.0992524510002113s]

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.

ExampleEdit & Run
import inspect

def add(a, b):
    return a + b

print(inspect.getsource(add))
Output:
True True False [Finished in 0.0877872130004107s]

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.

ExampleEdit & Run
import inspect

def add(a, b):
    return a + b

print(inspect.getsourcelines(add))
Output:
OSError: could not get source code [Finished in 0.07644643400271889s]

Inspecting Function and Method Signatures 

inspect.signature() 

Provides a Signature object containing parameter details (name, kind, default values).

ExampleEdit & Run
import inspect

def add(a, b=2, c = 3):
    return a +b + c

sig = inspect.signature(add)
print(sig)

for name, param in sig.parameters.items():
    print(f"{name}: {param.default if param.default != param.empty else 'No default'}")
Output:
OSError: could not get source code [Finished in 0.06707398800062947s]

Class and inheritance Inspection

inspect.getmro()  

Returns a tuple of classes in the method resolution order (MRO).

ExampleEdit & Run
import inspect

class A:
    pass

class B(A):
    pass

print(inspect.getmro(B))
Output:
(a, b=2, c=3) a: No default b: 2 c: 3 [Finished in 0.06715512799928547s]

inspect.isabstract() 

Determines if a class or method is abstract (part of abc module).

ExampleEdit & Run
import inspect
from abc import ABC, abstractmethod

class AbstractClass(ABC):
    @abstractmethod
    def method1(self):
        pass

print( inspect.isabstract(AbstractClass) )
Output:
(<class '__main__.B'>, <class '__main__.A'>, <class 'object'>) [Finished in 0.06849856899862061s]

Working with Frames and Call Stacks

inspect.currentframe() 

Retrieves the current frame object for debugging purposes.

ExampleEdit & Run
import inspect

def example():
    frame = inspect.currentframe()
    print(f"Function: {frame.f_code.co_name}")
    print(f"File: {frame.f_code.co_filename}")
    print(f"Line: {frame.f_lineno}")

example()
Output:
True [Finished in 0.07409633299903362s]

inspect.stack()

Returns a list of frame records for the current call stack.

ExampleEdit & Run
import inspect

def func1():
    func2()

def func2():
    for frame_info in inspect.stack():
        print(frame_info.function)

func1()
Output:
Function: example File: <string> Line: 12 [Finished in 0.06999954999992042s]

Analyzing objects dynamically  

inspect.getmembers() 

Returns all attributes and methods present in an object.

ExampleEdit & Run
import inspect

class Demo:
    attr = 10
    def method(self):
        pass

for name, value in inspect.getmembers(Demo):
    if not name.startswith('_'):
       print(f"{name}: {value}")
Output:
func2 func1 <module> [Finished in 0.07176953100133687s]

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.

ExampleEdit & Run
import inspect

for method in filter(lambda x: x.islower() and not x.startswith('_') , dir(inspect) ):
    print(method, end = '    ')
Output:
attr: 10 method: <function Demo.method at 0x7fd61249db20> [Finished in 0.07011445999887655s]