
What’s inside Python ‘for’ loop? - atulkrishna10
https://theailearner.com/2018/10/08/whats-inside-python-for-loop/
======
progval
Fun 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.

------
Tomminn
I'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
That’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
Duh- 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?

~~~
newen
Python's official implementation is very slow and they don't care about
performance. I wouldn't try to take any lessons from the way Python is
implemented.

------
kierenj
What'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
Python 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
I pray for the day that Tensorflow has an official rust (or scala) api.

------
theamk
The "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.

