3 ms·
Generally speaking, things that use break can be cleaned up by using python's `any` or `all` constructs over a slightly reworded loop. This isn't always true, b
by joshuamorton 8y ago
Generally speaking, things that use break can be cleaned up by using python's `any` or `all` constructs over a slightly reworded loop. This isn't always true, but handles most of the common cases.
I don't think that I've yet come across a use of break/continue that can't be pretty cleanly refactored into a helper method like
def my_filter(items):
for x in items:
if cond(x):
yield x
for x in my_filter(base_list):
process(x)
This ends up being pretty clean and describes exactly what/why you're filtering things, plus as the conditions change you can test things individually.
- maxbond 8y agoOne might also consider using something like `map(process, filter(cond, items))`