Can you help me with the below code? Urgent!

Pierre Quentel quentel.pierre at wanadoo.fr
Sun Jun 25 04:53:57 EDT 2006


This is because in "states" you store a reference to frame.f_locals,
not the value it takes. When you print states, all the items are the
same reference to the same object and have the same value

If you want to store the values at each cycle you should store a copy
of frame.f_locals, which will give you a different object

After import sys add the line :
    import copy

and instead of
    states.append(frame.f_locals)
write
    states.append(copy.copy(frame.f_locals))


Another example of this side-effect of storing references and not
values :

Python 2.4.2 (#67, Sep 28 2005, 12:41:11) [MSC v.1310 32 bit (Intel)]
on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> states = []
>>> x = [0]
>>> for i in range(10):
...     x[0] = i
...     states.append(x)
...
>>> print states
[[9], [9], [9], [9], [9], [9], [9], [9], [9], [9]]
>>>

Pierre




More information about the Python-list mailing list