Extract the body of a function or method’s source code, excluding its name,
docstrings, and comments.Parameters:
Name
Type
Default
Description
func_or_method
tp.Callable
--
The function from which to extract the source code body.
Returns:
Type
Description
str
The source code of the function body, with function name, docstrings, and comments removed. Each line is stripped of leading/trailing whitespace, and empty lines are excluded.
Raises:
Type
Description
OSError
If the source code of the function cannot be retrieved (e.g., for built-in functions).
SyntaxError
If the function’s source code cannot be parsed into an AST.
Examples:python
>>> def example_function(x):... '''This is a docstring.'''... y = x + 1 # Add 1 to x... print(y) # Print the result... return y # Return the result...>>> print(get_function_body(example_function))y = x + 1print(y)return y