On Wed, Nov 19, 2014 at 12:15 PM, Steven D'Aprano steve@pearwood.info wrote:
and any use of a loop that appends to a list is rightly considered code smell.
I'm afraid I don't understand that comment. Why is appending to a list inside a loop a code smell? That's exactly what list comps do.
That's precisely why. If I write code like this:
l = [] for i in something: l.append(func(i))
then I should rework it into a comprehension. Having a filter doesn't change that:
l = [] for i in something: if i: l.append(func(i))
That's still possible with a list comp, and should be rewritten as one. But having a break in there *does* change it, because there's no way in the language to do that. The question is: Is it better to abuse StopIteration or to turn the list comp back into an explicit loop? And if anyone chose the former, their code will break.
ChrisA