For loop extended syntax

Heiko Wundram modelnine at
Sun Mar 20 22:09:58 CET 2005

On Sunday 20 March 2005 20:47, George Sakkis wrote:
> Not always. Say for example that you're doing some 2D geometry stuff, and
> later you have to extend it to 3D. In this case you may have to deal with
> both 2D and 3D objects, and map the former to the latter when necessary.

But this rather sounds like you'd want an adaptor iterator, like the 

>>> class AdaptPossible2D(object):
...   def __init__(self,data):
... = data
...   def __iter__(self):
...     for item in
...       if len(item) == 2:
...         yield item+(0,)
...       else:
...         yield item
>>> for x,y,z in AdaptPossible2D([(1,2),(1,2,3),(3,4)]):
...   print x,y,z
1 2 0
1 2 3
3 4 0

Using the above code makes it absolutely clear what you want, and doesn't need 
any new syntax which can be ambiguous like (x=0,y,z=0), etc. The above idiom 
also takes only constant extra space, as it doesn't duplicate the list during 

--- Heiko.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 196 bytes
Desc: not available
URL: <>

More information about the Python-list mailing list