[Python-ideas] Variations on a loop
Bruce Frederiksen
dangyogi at gmail.com
Fri Aug 29 19:53:01 CEST 2008
Bruce Leban wrote:
>
>
> On Fri, Aug 29, 2008 at 6:19 AM, Mathias Panzenböck
> <grosser.meister.morti at gmx.net <mailto:grosser.meister.morti at gmx.net>>
> wrote:
>
> Bruce Frederiksen wrote:
> > We already have statements that only apply within loops (break and
> > continue), how about some expressions that only apply in for loops:
> > 'last' and 'empty':
>
> I don't know. I don't like either. What about named loops/loop
> objects? Using
> this you have no need for new keywords:
>
> myloop = for x in items:
> result += str(x)
> if not myloop.last:
> result += ', '
> if myloop.empty:
> result = 'no data'
>
>
> This is interesting. I don't like the fact that you have the loop
> variable scope extending past the end of the loop and it's not clear
> what we can do with that. It occurs to me that we can do something
> like this without changes to the language but it's clumsy:
>
> for x in funky(items):
> result += str(x.value)
> if not x.last():
> result += ', '
> else:
> if x == funky_empty:
> result = 'no data'
>
So x.first seems to make more sense than x.last. You could also name
the loops like:
outer = funky(items1)
for x in outer:
middle = funky(items2)
for y in middle:
inner = funky(items3)
for z in inner:
middle.continue_()
It seems to implement this, funky.continue_ and funky.break_ would have
to raise exceptions. If the for loop honored the 'throw' method (of
generators), it seems like this might be made to work...
-bruce
More information about the Python-ideas
mailing list