recipe-362305.py 文件源码

python
阅读 28 收藏 0 点赞 0 评论 0

项目:code 作者: ActiveState 项目源码 文件源码
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
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号