How can a function’s body be inspected in Python?
def main():
def example_function():
x = 4
y = 3
if x + y < 6:
print("Test")
print("Example")
import inspect
print(inspect.getsource(example_function))
main()
This prints out:
def example_function():
x = 4
y = 3
if x + y < 6:
print("Test")
print("Example")
Is there a robust way of only printing the function body without it’s signature?
Which would only output:
x = 4
y = 3
if x + y < 6:
print("Test")
print("Example")
In most cases skipping the first line would be sufficient, but it’s not guaranteed the function signature isn’t multi-line so a way to skip any signature would be better.