Re: Peculiar swap behavior

2009-03-05 Thread Aaron Brady
On Feb 23, 12:43 pm, Tim Chase wrote: > I stumbled across this oddity and was hoping folks on the list > might be able to provide a little understanding: > > # swap scalars >  >>> x,y = 1,2 >  >>> x,y = y,x >  >>> x,y > (2, 1) > > # swap lists >  >>> a,b = [1,2,3],[4,5,6] >  >>> a,b = b,a >  >>> a

Re: Peculiar swap behavior

2009-03-04 Thread Lie Ryan
andrew cooke wrote: Delaney, Timothy (Tim) wrote: Tim Chase wrote: # swap list contents...not so much... >>> m,n = [1,2,3],[4,5,6] >>> m[:],n[:] = n,m >>> m,n ([4, 5, 6], [4, 5, 6]) [...] For these types of things, it's best to expand the code out. The appropriate expansion of: m,n = [

Re: Peculiar swap behavior

2009-03-04 Thread Terry Reedy
Tim Chase wrote: I stumbled across this oddity and was hoping folks on the list might be able to provide a little understanding: # swap scalars >>> x,y = 1,2 >>> x,y = y,x >>> x,y (2, 1) # swap lists >>> a,b = [1,2,3],[4,5,6] >>> a,b = b,a >>> a,b ([4, 5, 6], [1, 2, 3]) # swap list cont

Re: Peculiar swap behavior

2009-02-23 Thread Gabriel Genellina
En Mon, 23 Feb 2009 22:58:44 -0200, John Posner escribió: m,n = [1,2,3],[4,5,6] m[:],n[:] = n,m I believe this is an RTFM situation. In the Python 2.6.1 help topic "Simple Statements" > "Assignment Statements", see this para: Yes, the other relevant paragraph is: """An assign

Re: Peculiar swap behavior

2009-02-23 Thread John Posner
>> m,n = [1,2,3],[4,5,6] >> m[:],n[:] = n,m I believe this is an RTFM situation. In the Python 2.6.1 help topic "Simple Statements" > "Assignment Statements", see this para: If the target is a slicing: The primary expression in the reference is evaluated. It should yield a mutable seq

RE: Peculiar swap behavior

2009-02-23 Thread andrew cooke
andrew cooke wrote: > Delaney, Timothy (Tim) wrote: >> Tim Chase wrote: >>> # swap list contents...not so much... >>> >>> m,n = [1,2,3],[4,5,6] >>> >>> m[:],n[:] = n,m >>> >>> m,n >>> ([4, 5, 6], [4, 5, 6]) > [...] >> For these types of things, it's best to expand the code out. The >> appropriat

Re: Peculiar swap behavior

2009-02-23 Thread Chris Rebert
On Mon, Feb 23, 2009 at 10:43 AM, Tim Chase wrote: > # swap list contents...not so much... m,n = [1,2,3],[4,5,6] m[:],n[:] = n,m m,n > ([4, 5, 6], [4, 5, 6]) Pseudo-C-Python expansion: #evaluate RHS. simply *take pointers* since the RHS is just plain variables ptr_n = &n ptr_m = &

Re: Peculiar swap behavior

2009-02-23 Thread Robert Kern
On 2009-02-23 16:17, andrew cooke wrote: Delaney, Timothy (Tim) wrote: Tim Chase wrote: # swap list contents...not so much... >>> m,n = [1,2,3],[4,5,6] >>> m[:],n[:] = n,m >>> m,n ([4, 5, 6], [4, 5, 6]) [...] For these types of things, it's best to expand the code out. The appropriat

RE: Peculiar swap behavior

2009-02-23 Thread andrew cooke
Delaney, Timothy (Tim) wrote: > Tim Chase wrote: >> # swap list contents...not so much... >> >>> m,n = [1,2,3],[4,5,6] >> >>> m[:],n[:] = n,m >> >>> m,n >> ([4, 5, 6], [4, 5, 6]) [...] > For these types of things, it's best to expand the code out. The > appropriate expansion of: > m,n = [1,2

RE: Peculiar swap behavior

2009-02-23 Thread Delaney, Timothy (Tim)
Tim Chase wrote: > # swap list contents...not so much... > >>> m,n = [1,2,3],[4,5,6] > >>> m[:],n[:] = n,m > >>> m,n > ([4, 5, 6], [4, 5, 6]) > > > The first two work as expected but the 3rd seems to leak some > internal abstraction. It seems to work if I force content-copying: > > >>> m[:

Peculiar swap behavior

2009-02-23 Thread Tim Chase
I stumbled across this oddity and was hoping folks on the list might be able to provide a little understanding: # swap scalars >>> x,y = 1,2 >>> x,y = y,x >>> x,y (2, 1) # swap lists >>> a,b = [1,2,3],[4,5,6] >>> a,b = b,a >>> a,b ([4, 5, 6], [1, 2, 3]) # swap list contents...not so much... >>