[issue35966] Didn't raise "StopIteration" Error when I use "yield" in the function

sheiun report at bugs.python.org
Mon Feb 11 08:31:49 EST 2019


sheiun <me.sheiun at gmail.com> added the comment:

But it still can catch by using try/except

>>> def generate_loop_stopiteration():
...     for i in range(10):
...         print(i)
...         # should raise StopIteration when i > 5
...         try:
...             k = next(j for j in range(5) if j == i)
...         except StopIteration:
...             print('catch')
...         print(k)
...         yield k
...
>>> print(list(generate_loop_stopiteration()))
0
0
1
1
2
2
3
3
4
4
5
catch
4
6
catch
4
7
catch
4
8
catch
4
9
catch
4
[0, 1, 2, 3, 4, 4, 4, 4, 4, 4]
>>>

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue35966>
_______________________________________


More information about the Python-bugs-list mailing list