I almost posted this in “Ideas”, but I assume that there must be some design logic here that I have missed:
When an iterable ends, it throws a StopIteration
exception. StopIteration
exceptions hold a value
(defaulting to None
). This value
can help indicate a final result before ending (such as using return
inside a generator function). itertools.tee
is designed to ““copy”” an iterator, however the iterables created by tee
do not retain the StopIteration object thrown by the original iterator, nor even the value
it held. Rather, it creates fresh StopIteration
objects with value=None
to throw instead.
Why? Wouldn’t it be better to save the original StopIteration
object to throw for each child iterator? Or if mutability is a concern, then at least retain its value
to construct the new StopIteration
with? This issue also means that iterators that end with a subclass of StopIteration
, if tee
d, will no longer produce this subclass anymore, potentially losing even more data.