Trace Table for Python Programs

前端 未结 3 1322
我在风中等你
我在风中等你 2021-02-09 12:04

Is there a way to get the trace table for a Python program? Or for a program to run another program and get its trace table? I\'m a teacher trying to flawlessly verify the answe

3条回答
  •  野性不改
    2021-02-09 12:28

    This isn't a use case that the current Python tracing tool support, but it should be possible to build. I don't know how you decide what columns to output. In your sample, a and b are the only local variables, but larger programs would have more interesting ecosystems of data.

    Updated: here's a simple proof of concept:

     1     import sys
     2
     3     def trace(frame, event, arg_unused):
     4         print event, frame.f_lineno, frame.f_locals
     5         return trace
     6
     7     sys.settrace(trace)
     8
     9     def foo():
    10         a = 1
    11         b = 2
    12
    13         a = a + b
    14
    15     foo()
    

    when run, the output is:

    call 9 {}
    line 10 {}
    line 11 {'a': 1}
    line 13 {'a': 1, 'b': 2}
    return 13 {'a': 3, 'b': 2}
    

提交回复
热议问题