5 ms·
> in python `for` loop are equally unbounded as `while` ones. Yes, most of the time you are looping over a (finite) list or a (finite) dict, but the concept und
by ghgr 5y ago
> in python `for` loop are equally unbounded as `while` ones. Yes, most of the time you are looping over a (finite) list or a (finite) dict, but the concept underlying the `for` loop is the iterator, not the list. And iterators can easily be infinite.
Not only iterators, but also plain lists. Example:
a=[1]
for i in a:
a.append("the ride never ends")
print(i)
- deleted 5y ago[deleted]
- klyrs 5y agoDid you try that out in a REPL before posting? You should.
- shhsshs 5y agoI'm guessing he did, because it does exactly what he's implying it does.
- klyrs 5y agowow, my bad. That's sets & dicts that invalidate their iterators when the size changes.
- marcosdumay 5y agoIterating over mutable unordered containers is an entire new level of crazy.
- klyrs 5y agoThat's what linters are for, right? To make your code more fun to read