def get_function_body(func):
source_lines = inspect.getsourcelines(func)[0]
source_lines = dropwhile(lambda x: x.startswith('@'), source_lines)
def_line = next(source_lines).strip()
if def_line.startswith('def ') and def_line.endswith(':'):
# Handle functions that are not one-liners
first_line = next(source_lines)
# Find the indentation of the first line
indentation = len(first_line) - len(first_line.lstrip())
return [first_line[indentation:]] + [line[indentation:] for line in source_lines]
else:
# Handle single line functions
return [def_line.rsplit(':')[-1].strip()]
评论列表
文章目录