On Feb 23, 12:43 pm, Tim Chase python.l...@tim.thechases.com 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
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 contents...not so
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 =
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[:],n[:] = n[:],m[:]
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,3],[4,5,6]
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
appropriate
On Mon, Feb 23, 2009 at 10:43 AM, Tim Chase
python.l...@tim.thechases.com wrote:
snip
# 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
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 =
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:
snip
If the target is a slicing: The primary expression in the reference is
evaluated. It should yield a mutable
En Mon, 23 Feb 2009 22:58:44 -0200, John Posner jjpos...@snet.net
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:
10 matches
Mail list logo