1

(In Python 3.2)

miters = map(abs,(-1,2,5))
for i in miters:
    print(i)
1
2
5

next(miters)
--> StopIteration

for i in miters:
    print(i)
--> ?? Nothing Happens..

Why can't I get 'StopIteration' Error Message in the second for loop? 'miter' has been exhausted, so if I loop it again, I think there would be a 'StopIteration'...

Can any one tell me why?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
nemonein
  • 41
  • 2
  • 6

1 Answers1

4

The for loop uses StopIteration to work out when to stop. So it swallows the exception and stops immediately.

Jon Clements
  • 138,671
  • 33
  • 247
  • 280