for <var> in <sequence> if <condition>

Dave Brueck dbrueck at edgix.com
Thu Oct 5 14:33:44 EDT 2000


Dave wrote:
> >What's wrong with
> >
> >    for line in lines:
> >        if line.strip().startswith('y'):
> >           doStuff(line)
>
> Nothing at all. But then again, there's nothing wrong with 'x = x + 1'
> instead of 'x += 1', either. :)

>if you're dealing with mutable objects, += might be something
>completely different.
>compare:
> [snip]

I wouldn't say they are 'completely' different, and using the longer version
is certainly not 'wrong' (The in-place operation of += is probably closer to
an implementation nuance than an actual part of the language, even if it is
documented).

Although I was thinking in terms of syntactic sugar (or near-sugar, it
seems.. synactic NutraSweet?), a similar situation could apply to the
enhanced 'for' construct: In most cases they may behave the same but in the
new case fewer objects would need to be created or something.

Anyway, I'm not on the campaign trail to add it to the language, just
thought it might be interesting.

-Dave





More information about the Python-list mailing list