G
gokcemutlu
I want to trace a function while it executes and keep its local
variables as states. In trace function all the things work well but
after all when I print states, they are all the same. I couldn't solve
the problem. I guess it's because of frame, please can you help me?
import sys
states = []
def traceit(frame, event, arg):
location = frame.f_code.co_name
if location == "afunc":
states.append(frame.f_locals)
print frame.f_locals
return traceit
def afunc(a, b, c):
x = a + b
y = b + c
z = b
rslt = x + y - z
return rslt
print "This is what I should see!"
sys.settrace(traceit)
afunc(1, 2, 3)
sys.settrace(None)
print "\nWhy does this happen and how can I solve it?"
for s in states:
print s
variables as states. In trace function all the things work well but
after all when I print states, they are all the same. I couldn't solve
the problem. I guess it's because of frame, please can you help me?
import sys
states = []
def traceit(frame, event, arg):
location = frame.f_code.co_name
if location == "afunc":
states.append(frame.f_locals)
print frame.f_locals
return traceit
def afunc(a, b, c):
x = a + b
y = b + c
z = b
rslt = x + y - z
return rslt
print "This is what I should see!"
sys.settrace(traceit)
afunc(1, 2, 3)
sys.settrace(None)
print "\nWhy does this happen and how can I solve it?"
for s in states:
print s