外部函数的调试包装器

2024-09-30 01:22:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在寻找一种同时打印和执行一个函数/方法的方法,就像我可以包装一个函数一样。问题是我不能直接修饰函数,因为我调用的函数是jython模块的一部分。 所以我有一些线索

from jythonModule import fun, obj

fun(a,b,c)
o = obj
o.method(e,f)

我正在寻找离开运行和打印代码 所以它会显示

有趣(a、b、c) o、 方法(e,f)

然后执行这些命令。 如果不访问jython模块,我怎么能做到这一点呢? 干杯


Tags: 模块方法函数代码fromimport命令obj
1条回答
网友
1楼 · 发布于 2024-09-30 01:22:34

您可以使用^{}

# trace.py
import sys


def trace_function(frame, event, arg):
    if event == "call":  # Only report function calls
        code_name = frame.f_code.co_name
        code = frame.f_code
        print "Function call: {0} @ {1}:{2}".format(code.co_name, code.co_filename, code.co_firstlineno)
        print "Locals:", frame.f_locals
        print
    return trace_function  # Continue tracing the new scope



def f0(arg):
    print "In f0: ", arg
    print "Done"

def f1(arg):
    print "In f1: ", arg
    f0(arg)

def f2(arg):
    print "In f2: ", arg
    f1(arg)


sys.settrace(trace_function)


f2("The arg string")

将提供以下输出:

^{pr2}$

相关问题 更多 >

    热门问题