![]() |
VOOZH | about |
The inspect module in Python is useful for examining objects in your code. Since Python is an object-oriented language, this module helps inspect modules, functions and other objects to better understand their structure. It also allows for detailed analysis of function calls and tracebacks, making debugging easier. The module provides methods for verifying the type of an object and retrieving its source, with common methods categorized accordingly.
Inspect module offers methods to verify the type of an object, such as checking if it's a class, module, function, or method. These tools help with introspection and understanding objects in your code.
Method | Description |
|---|---|
isclass(obj) | Returns True if obj is a class |
ismodule(obj) | Returns True if obj is a module |
isfunction(obj) | Returns True if obj is a function |
ismethod(obj) | Returns True if obj is a method |
Example 1: In this example, we are checking whether a given object is a class or not. We define a simple class A and then pass it to inspect.isclass(). If the passed object is indeed a class, the method returns True.
True
Example 2: In this example, we are checking whether a given object is a module or not. We import the numpy module and then pass it to inspect.ismodule(). If the object passed is a module, the method returns True.
True
Example 3: In this example, we check if an object is a user-defined function. The function fun returns double the input. Passing it to isfunction() returns True since it's a valid function.
True
Example 4: In this example, we use inspect.ismethod() to check if collections.Counter is a method. Since Counter is a class and not a method, ismethod() returns False.
False
Inspect module also provides methods to retrieve the source of an object, such as inspecting class hierarchies, member functions, or function signatures. These tools help you understand the structure and source details of objects in your code.
Method | Description |
|---|---|
getclasstree(classes) | Returns a nested list of tuples showing the class hierarchy |
getmembers(obj) | Returns all the members of an object as (name, value) pairs |
signature(callable) | Returns the call signature of a function/method |
stack() | Returns the call stack at the point where it's called |
getmodule(obj) | Returns the module in which an object was defined |
getdoc(obj) | Returns the docstring of the object |
Example 1: In this example, we inspect the class hierarchy of C, which inherits from B and B inherits from A. We use inspect.getmro(C) to get the method resolution order and pass it to inspect.getclasstree() to get a structured inheritance tree.
(<class 'object'>, ()) [(<class '__main__.A'>, (<class 'object'>,)), [(<class '__main__.B'>, (<class '__main__.A'>,)), [(<class '__main__.C'>, (<class '__main__.B'>,))]]]
Example 2: In this example, we use inspect.getmembers() to fetch members of the math module and print the names and types of the first five.
__doc__ <class 'str'> __file__ <class 'str'> __loader__ <class '_frozen_importlib_external.ExtensionFileLoader'> __name__ <class 'str'> __package__ <class 'str'>
Example 3: In this example, we use inspect.signature() to get the signature of the greet function, showing its parameters and default values.
(name, age=18)
Example 4: In this example, we use inspect.stack() to retrieve the current call stack. We then print the function names and line numbers of the top two stack frames.
Function: test, Line No: 4 Function: <module>, Line No: 8
Example 5: In this example, we use inspect.getmodule() to retrieve the module that the math.sin function belongs to. We then print the name of the module using m.__name__.
math
Example 6: In this example, we use inspect.getdoc() to fetch the docstring of the fun function, which describes its purpose returning the product of x and y.
Returns the product of x and y.