For loop extended syntax

Jeff Shannon jeffshannon at gmail.com
Mon Mar 21 16:13:09 EST 2005


George Sakkis wrote:

> A generalization of the 'for .. in' syntax that would handle 
 > extra arguments the same way as functions would be:
> 
> for (x,y,z=0,*rest) in (1,2,3), (3,4), (5,6,7,8):
>      print x, y, z, rest
> 
> I'd love to see this in python one day; it is pretty obvious what
 > it would do for anyone familiar with function argument tuples.

Let's all keep in mind that for...in... is using standard tuple 
unpacking rules (you're getting the next tuple from a list of tuples, 
and then unpacking that tuple), so what is really being proposed are 
extensions to tuple unpacking.  (Making this work in the context of a 
for loop but not work in other tuple-unpacking situations would create 
inconsistency.)

Function arguments are *not* (in general) a case of tuple unpacking, 
on the other hand, so the parallels between function arguments and for 
loop control-variable tuples are not so straightforward as is being 
claimed.

There may be valid arguments in favor of enhancing tuple unpacking in 
this way (indeed, I believe I recall a thread or two on this subject), 
but it's important to consider the general consequences, not just the 
single aspect of for-loop usage.

Jeff Shannon




More information about the Python-list mailing list