More than one element of list changing when only one should be

Chuckk Hubbard badmuthahubbard at gmail.com
Sun Jun 8 02:28:14 EDT 2008


I solved this problem.  I still don't understand why, but it seems
Python was appending a reference to the first element at the end of
the list, rather than copying the data.  I used something like:
"self.regionlist.append(list(self.regionlist[self.hregion])"
and now it works fine, i.e., altering the appended list member doesn't
change the copied one.

-Chuckk


On Sat, Jun 7, 2008 at 5:53 PM, Chuckk Hubbard
<badmuthahubbard at gmail.com> wrote:
> Hello.
> This program is clunky, I know; I'm not a programmer, but I need to
> use this program, so I'm writing it.
> The problem:
> I have a cursor following the mouse that shows frequency ratios of
> potential notes in relation to 1/1 (something like Middle C).  At any
> time, the user may hit "t" to move 1/1 to wherever the cursor is.
> There is also the option to use many regions, so that some of the
> notes in the score, in region 0, for instance, can have 1/1 as their
> base, and others, in region 1 for instance, could have perhaps 3/2 as
> their base.
>
> The program starts out with 2 existing regions, region 0 = 1/1, and
> region 1 = 3/2.  If the user hits r+NUM, the cursor switches to region
> NUM.  If NUM is longer than the list of regions (self.regionlist), a
> new region is appended with the same base as the current one, and the
> cursor goes to that region.
>
> SO, if you start this program, then:
> 1) move the cursor around a little;
> 2) hit 'r' and '1' at the same time - now you are in region 1;
> 3) hit 'r' and '0', now region 0;
> 4) hit 'r' and '2', now a new region 2 is created with the same
> parameters as region 0, and self.regionlist is appended with the new
> info - now you're in region 2;
> 5) move the mouse until the fraction reads anything other than 1/1;
> 6) hit 't' to transpose the current region by that fraction;
>
> You can see by the output in the text window that self.regionlist[0]
> AND self.regionlist[2] have been updated.  Only [2] should have been
> changed.
>
> 7) hit 'r' and '0', and see that region 0 has now changed its base to
> match region 2.
>
> I hope someone is curious enough to get through this and help me.  I
> tried extracting the function in question into its own mini-file and
> the problem didn't happen.  I can't think of any reason these lines:
>
>        self.regionlist[self.hregion][0] = self.curnum
>        self.regionlist[self.hregion][1] = self.curden
>        self.regionlist[self.hregion][3] = self.octave11 = self.yadj
>
> should change self.regionlist[0] AND self.regionlist[2] in the same
> call, but they do.  Also, if I add more regions in series, they all
> update each other.
>
> Thanks for your time.
>
> -Chuckk
>
> --
> http://www.badmuthahubbard.com
>



-- 
http://www.badmuthahubbard.com



More information about the Python-list mailing list