3 ms·
While I appreciate the idea of bounded loops, it's a mistake to think that a `for` loop is necessarily bounded. It's pretty easy to write a generator that prod
by nine_k 5y ago
While I appreciate the idea of bounded loops, it's a mistake to think that a `for` loop is necessarily bounded.
It's pretty easy to write a generator that produces an infinite sequence. That generator, of course, would need a `while` loop, but that loop can be in an external dependency which the linter is not checking.
You also can read way more than you expected if you use a `for` loop to read from a socket, or a file, to say nothing about reading from /dev/random.
Not that it's a bad advice, but it's not a rule, it's more like a... guideline. Discretion is still needed.
- choeger 5y agoYou should be able to create an unbounded for loop as well. After all, a python process has mutable state, so it should be fairly simple to increment the length of your loop inside the loop body.
- uryga 5y ago>>> x = [0] >>> for x in xs: ... print(x) ... xs.append(x+1)
- toxik 5y agoOr bounded… by the heat death of the universe. I think there’s a point though, if you’re writing a stack machine or something like that where it makes sense, you’d know. But many novice programmers reach for the old while-bool-var construct and set it to false in the loop body (and often reset it to true later. So confusing. )