<div dir="ltr"><br><br><div class="gmail_quote">On Fri, Aug 29, 2008 at 6:19 AM, Mathias Panzenböck <span dir="ltr"><<a href="mailto:grosser.meister.morti@gmx.net">grosser.meister.morti@gmx.net</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"><div class="Ih2E3d">Bruce Frederiksen wrote:<br>
> We already have statements that only apply within loops (break and<br>
> continue), how about some expressions that only apply in for loops:<br>
> 'last' and 'empty':<br>
<br>
</div>I don't know. I don't like either. What about named loops/loop objects? Using<br>
this you have no need for new keywords:<br>
<br>
myloop = for x in items:<br>
result += str(x)<br>
if not myloop.last:<br>
result += ', '<br>
if myloop.empty:<br>
result = 'no data'<br>
</blockquote><div><br>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:<br>
<br>for x in funky(items):<br> result += str(x.value)<br> if not x.last():<br> result += ', '<br>else:<br> if x == funky_empty:<br> result = 'no data' <br><br></div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<br>
However, how to calculate the last property for generators where it might depend<br>
on some side effects if it's really the last item?<br>
</blockquote><div><br> Note that I wrote x.last() not x.last: when you check last, it pre-fetches the next value. That's not an issue in the above code but could be in other cases, so you have to be careful when you use it.<br>
<br>This could also provide other things like automatic counting of loop iterations:<br><br>for x in funky(items):<br> foo(x.index, x.value)<br><br>It wouldn't provide the ability to do nested breaks and continues which I'd like to see.<br>
<br>--- Bruce<br></div></div></div>