3 ms·
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.
by progval 8y ago
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.