Python change a value of a variable by itself.
apt.shansen at gmail.com
Tue Feb 17 17:28:14 CET 2009
On Tue, Feb 17, 2009 at 8:19 AM, Kurda Yon <kurdayon at yahoo.com> wrote:
> r_new = r
This is the problem. "r" is a dictionary, a set of key/object pairs in
essence. You're making the object that "r" is pointing to a list, a
mutable sequence of items.
The expression "r" will then return that list object and assign it
to "r_new" -- but now both these two dictionaries are pointing to
the *same* object. Its not a copy of the object, but the same object
itself which you're storing in two different dictionaries.
If you want to store a copy of that list in r_new, you can use the
copy module, or something like:
r_new = r[:]
which uses list slices to return a copy of the specified list.
More information about the Python-list