Help! This is driving me insane!

Mike C. Fletcher mcfletch at rogers.com
Thu Sep 26 21:13:08 EDT 2002


You do two prints:
	[0, 0]
	[0, 0]

Then somehow or another you're getting the result of the whole 
method-invocation printed (likely by the interactive interpreter), which is:

	[[1,1]]

After the method has returned, your "grouptotalcounter" code has been 
called, so there's no mystery there for Python peoples.  The list 
grouptotal's values were updated, just as your method said to do, and 
that list is the first item in the errorlog list you returned.

I _think_ the problem you're having is that you're thinking by appending 
the list object to self.errorlog you are somehow "fixing" the values in 
the list object as those when appended (which is something you would see 
in, for example C, where you were assigning to arrays).

i.e.

	a = []
	b = [1,2]
	a.append(b)
	b[0] = 3

would end with a == [[3,2]] and b == [3,2].

Because a[0] _is_ b, _not a copy_ of b.  That is, a[0] is the same 
_object_ as pointed to by b, so any changes to b after adding it to a 
will show up in a (since it's just pointing to same list b).

Try using a.append( b[:] ) if you want to append a copy of b, rather 
than the original list.  Note, however, that if you're doing nested lists:

	a = [[],[],[]]

You'll want to use the copy module explicitly for creating copies 
(otherwise only the top-level list is copied, the lower-level lists are 
still just referenced in the new list).

HTH,
Mike

kara.klosterman wrote:
...
> http://www.geocities.com/dbasch/insane.txt
> 
> I would expect to get:
> [0, 0]
> [0, 0]
> [[0, 0]]
> 
> but I am getting:
> [0, 0]
> [0, 0]
> [[1, 1]]
> 
> I dont understand how the 'grouptotals' list is
> getting the incremented values prior to the
> 'grouptotalcounter' function being called.
> furthermore, I can place a 'print grouptotals' right
> after the errorlog append and it isnt the incremented
> values! Please tell me this isnt a normal thing!
> Thanks,
> Derek Basch
> 
> 
> 

-- 
_______________________________________
   Mike C. Fletcher
   Designer, VR Plumber, Coder
   http://members.rogers.com/mcfletch/






More information about the Python-list mailing list