103. Python's inspect Module
1. Getting Information About Functions Using getmembers
getmembersimport 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)2. Retrieving Function Source Code Using getsource
getsourceimport 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)3. Inspecting the Function's Signature Using signature
signature4. Inspecting the Parameters of a Function Using parameters
parameters5. Checking if a Function is a Generator Using isgeneratorfunction
isgeneratorfunction6. Checking if an Object is a Built-in Function or Method Using isbuiltin
isbuiltin7. Getting the Class of a Function or Method Using getclass
getclass8. Checking if an Object is Callable Using callable
callable9. Getting Information About the Stack Trace Using stack
stack10. Getting Information About the Current Line of Code Using currentframe
currentframeSummary:
Last updated