Bruce Frederiksen wrote:
Bruce Leban wrote:
result = "" for x in items: result += str(x) interstitially: result += ", " contrariwise: result = "no data"
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':
for x in items: result += str(x) if not last: result += ', ' else: if empty: result = "no data"
-bruce (not to be confused with Bruce :-)
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'
However, how to calculate the last property for generators where it might depend on some side effects if it's really the last item?
Other things would be possible, too:
a = for x in xs: b = for y in ys: c = for z in zs: if cond(z): break b if cond(y): continue a
However, this would increase clutter and might be a bad idea for the same reason goto is a bad idea. I also would be possible to pass the loop object to another function which then might call continue/break which changes the controlflow completely and makes the code very hard to read. So I guess this is a bad idea.