def rework(func):
"""
rework() modifies the functions source code by first adding self
as the first argument in the parameter list and then adding
'exec(magic())' as the first line of the function body. It does this
by reading the functions source code and then creating a new modified
function definition with exec() and then returns the new function.
"""
srclines, line_num = inspect.getsourcelines(func)
srclines = outdent_lines(srclines)
dst = insert_self_in_header(srclines[0])
if len(srclines) > 1:
dst += get_indent_string(srclines[1]) + "exec(magic())\n"
for line in srclines[1:]:
dst += line
dst += "new_func = eval(func.__name__)\n"
exec(dst)
return new_func
评论列表
文章目录