4 ms·
What’s inside Python ‘for’ loop?
- progval 8y agoFun side-effect of this: if you're writing a Python interpreter, you need to implement exceptions+"try...except" before you can fully implement the "for" loop. And to implement this "except", you need classes and basic inheritance. EDIT: the second code in the article is slightly wrong. The for loop is actually equivalent to: iter_object = iter(my_list) while True: try: number = next(iter_object) except StopIteration: break sum += number # <-- the body of the original loop is here, outside the "try" print(sum) because if the body of the original "for" loop raises a "StopIteration", the loop doesn't catch it.
- deleted 8y ago[deleted]
- kierenj 8y agoWhat's the overhead of exceptions like in Python? With .NET at least, especially with a debugger attached, this would have a pretty considerable impact!
- mehrdadn 8y agoPython is already so slow that it's comparable to normal iteration. But here's an example: timeit('next(x)', 'x = iter(range(1<<30))', number=1<<22) # 0.22 timeit('''try: raise StopIteration() except StopIteration: pass''', number=1<<22) # 0.78
- dijksterhuis 8y agoI pray for the day that Tensorflow has an official rust (or scala) api.
- Tomminn 8y agoI'm not a particularly experienced programmer. It is a sign of my immature taste to find this ugly? The idea of assuming any exception thrown will be the exception we expect (outside iteration limits) seems like an unnecessary abuse of the exception system. I'm really surprised it's the way such a fundamental construct in the language is written. Is the idea here that all other exceptions will be thrown at the line "iter_object= iter(my_list)"? I guess this makes sense. But it still makes me uncomfortable. Should I become more comfortable with writing constructs like this?
- dijksterhuis 8y agoThat’s not how it works. “except StopIteration” only watches for the specific StopIteration exception coming from the next() call on the Iterator (the bit included in the try block). If there was then an, for example, “except Error” line afterwards, then anything that causes an Error exception (ValueError, TypeError etc) would call those lines. If you get any exception that has not been explicitly referenced in an except clause, then the program will halt, with that exception as the error output.
- Tomminn 8y agoDuh- reading comprehension fail- cheers. Same question though, there is still something that makes me uncomfortable about using exceptions as an expected-case control flow structure (as in, this is expected to catch an exception once every for loop call). Is that something I should get over, and should I write more code that uses exceptions in this fashion?
- theamk 8y agoThe "inside" part is slightly wrong, the payload is outside of the try/except clause: while True: try: number = next(iter_object) except StopIteration: break sum += number You can check this by throwing StopIteration inside the loop, and watching it propagate outside normally.