why does this unpacking work

Gabriel Genellina gagsl-py at yahoo.com.ar
Fri Oct 20 17:06:09 EDT 2006


At Friday 20/10/2006 17:29, John Salerno wrote:

>I was expecting 't' to be a two-tuple for it to work. Maybe writing it as:
>
>for (x,y) in t
>
>sort of helps to show that '(x,y)' is equivalent to one object in 't'.
>That makes it look a little more cohesive in my mind, I guess, or helps
>me to see it map out against 't' better.

Note that it's the *comma* in an expression list what creates a 
tuple, *not* the parens. A similar rule applies on the target side of 
an assignment:

x,y = (1,2)
(x,y) = [1,2]
[x,y] = 1,2

and all variations are all equivalent.
The left part of a for statement is like an assignment.
With this in mind, it's not surprise that

for (x,y) in t: pass
for x,y in t: pass

are exactly the same.

<http://docs.python.org/ref/exprlists.html>
<http://docs.python.org/ref/assignment.html>


-- 
Gabriel Genellina
Softlab SRL 

__________________________________________________
Correo Yahoo!
Espacio para todos tus mensajes, antivirus y antispam ¡gratis! 
¡Abrí tu cuenta ya! - http://correo.yahoo.com.ar



More information about the Python-list mailing list