Peculiar swap behavior

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Mon Feb 23 21:02:04 EST 2009


En Mon, 23 Feb 2009 22:58:44 -0200, John Posner <jjposner at 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:

"""An assignment statement evaluates the expression list (remember that  
this can be a single expression or a comma-separated list, the latter  
yielding a tuple) and assigns the single resulting object to each of the  
target lists, from left to right."""

Note that the RHS is evaluated COMPLETELY before assignment to the LHS  
begins. The assignment:

a, b = c, d

is NOT equivalent to:

a = c
b = d

except in simple cases.

-- 
Gabriel Genellina




More information about the Python-list mailing list