For loop extended syntax
Heiko Wundram
modelnine at ceosg.de
Sun Mar 20 16:09:58 EST 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
following:
>>> class AdaptPossible2D(object):
... def __init__(self,data):
... self.data = data
... def __iter__(self):
... for item in self.data:
... 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
iteration.
--
--- Heiko.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 196 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20050320/2465e91a/attachment.sig>
More information about the Python-list
mailing list