103. Python's inspect Module

1. Getting Information About Functions Using getmembers

import inspect

def sample_function(x, y=10):
    """This is a sample function."""
    return x + y

# Get all the members of the function, including its parameters and docstring
members = inspect.getmembers(sample_function)
print(members)

Explanation: inspect.getmembers returns a list of all the members of the sample_function, including its parameters, docstring, and other information.


2. Retrieving Function Source Code Using getsource

import inspect

def sample_function(x, y=10):
    """This is a sample function."""
    return x + y

# Get the source code of the function
source_code = inspect.getsource(sample_function)
print(source_code)

Explanation: inspect.getsource retrieves the source code of the sample_function as a string, which can be useful for debugging or introspection.


3. Inspecting the Function's Signature Using signature

Explanation: inspect.signature retrieves the function signature, including the parameters and default values. It's useful for checking how a function should be called.


4. Inspecting the Parameters of a Function Using parameters

Explanation: The parameters attribute of a signature object returns an ordered mapping of parameter names to their details, including default values.


5. Checking if a Function is a Generator Using isgeneratorfunction

Explanation: inspect.isgeneratorfunction checks if a function is a generator function (i.e., a function that uses yield).


6. Checking if an Object is a Built-in Function or Method Using isbuiltin

Explanation: inspect.isbuiltin checks if the object is a built-in function or method in Python.


7. Getting the Class of a Function or Method Using getclass

Explanation: inspect.getclass retrieves the class to which a function or method belongs.


8. Checking if an Object is Callable Using callable

Explanation: callable checks if an object is callable (i.e., it can be invoked as a function). A class instance may also be callable if the __call__ method is defined.


9. Getting Information About the Stack Trace Using stack

Explanation: inspect.stack() returns a list of FrameInfo objects that contain the call stack, which is helpful for debugging and tracing code execution.


10. Getting Information About the Current Line of Code Using currentframe

Explanation: inspect.currentframe() gets the current stack frame, and f_lineno gives the line number where the code is currently executing.


Summary:

  • inspect.getmembers: Retrieve all members of an object (e.g., functions, methods, or classes).

  • inspect.getsource: Get the source code of a function or method.

  • inspect.signature: Retrieve the signature of a callable object (e.g., function or method).

  • inspect.isgeneratorfunction: Check if a function is a generator function.

  • inspect.isbuiltin: Check if an object is a built-in function.

  • inspect.getclass: Retrieve the class of a function or method.

  • callable: Check if an object is callable.

  • inspect.stack: Retrieve the call stack information.

  • inspect.currentframe: Retrieve the current stack frame.

These tools from the inspect module are useful for debugging, introspecting, and analyzing live objects and functions in Python.

Last updated