Trace Table for Python Programs

前端 未结 3 1276
我在风中等你
我在风中等你 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:35

    You could use the Python debugger, though I do not know how to have it step through on it's own, but it's likely do-able, then you could just parse the output.

    Here's a really crude example:

    adding.py

    a = 1
    b = 2
    
    a = a + b
    

    running it...

    PS >python -m pdb adding.py
    > adding.py(1)()
    -> a = 1
    (Pdb) alias stepprint step;;print a;;print b
    (Pdb) stepprint
    > adding.py(2)()
    -> b = 2
    1
    *** NameError: name 'b' is not defined
    (Pdb) stepprint
    > adding.py(4)()
    -> a = a + b
    1
    2
    (Pdb) stepprint
    --Return--
    > adding.py(4)()->None
    -> a = a + b
    3
    2
    (Pdb) stepprint
    --Return--
    > (1)()->None
    3
    2
    (Pdb) stepprint
    The program finished and will be restarted
    > adding.py(1)()
    -> a = 1
    *** NameError: name 'a' is not defined
    *** NameError: name 'b' is not defined
    (Pdb) q
    
    PS >
    

    End (q) on the "The program finished" bit.

提交回复
热议问题