Consider the following example:
def fn(x):
if x > 2:
raise StopIteration
return x
results = list(map(fn, range(5)))
print(results)
When I run this with python 2, I get what I expected:
Traceback (most recent call last):
File "example.py", line 5, in <module>
results = list(map(fn, range(5)))
File "example.py", line 3, in fn
raise StopIteration
StopIteration
However, if I run it with python 3, the program does not end with the StopIteration
exception. It prints the following result:
[0, 1, 2]
The map
function in python 3 (specifically python 3.5.1) seems to catch and handle the StopIteration
exception as though the provided iterable has thrown it. Is this a bug?