SetTrace () in Python

Is there a way to use the setTrace () function in a script that does not have method definitions? i.e.

for i in range(1, 100):
    print i

def traceit(frame, event, arg):
    if event == "line":
        lineno = frame.f_lineno
        print "line", lineno

return traceit

sys.settrace(traceit)

so I would like the trace function to be called on every iteration / line of code executed in a loop. I did this with scripts that previously had method definitions, but I'm not sure how to make it work on this instance.

+5
source share
2 answers

settrace () is really just for implementing debuggers. If you use it to debug this program, you might be better off using PDB

According to the documentation, settrace () will not do what you want.

, , AST Abstract, Python. , .

+2

:

import pdb; pdb.set_trace()

, . pdb (n , l ..).

,

.

+2

All Articles