Take a look at this, the crux of the question is at the bottom:
>>> scan = iter('FHUR203459')
>>> while True:
print(next(scan))
F
H
U
R
2
0
3
4
5
9
Traceback (most recent call last):
File "<pyshell#11>", line 2, in <module>
print(next(scan))
StopIteration
>>> scan = iter('FHUR203459')
>>> for i in range(12): # 12 * 2 for each join is 24, much longer than the string; should raise error.
print(''.join(next(scan) for i in range(2)))
FH
UR
20
34
59
>>>
In other words, we can see that the iterator reaches its end in both situations, however it only raises a StopIteration
in the first, even though next()
is used in both situations after it reached the end. Why does using it in join
seem to evade the error? Or is this a bug?