Can you help me with the below code? Urgent!

gokcemutlu at gmail.com gokcemutlu at gmail.com
Sun Jun 25 05:02:00 EDT 2006


Hello,

You're right about it but this is a simple code which tells my problem.
I need actually the frame itself for states and unfortunately
copy.copy(frame) throws an exception. Pickling also doesn't work. Do
you have any other idea?

Thanks,

Gokce.


Pierre Quentel schrieb:

> 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