-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
gh-137597: Clarify flattening and bugfix for itertools.tee #137599
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
eec6407
67fbdf2
d0317f7
b608303
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -730,6 +730,29 @@ loops that truncate the stream. | |
produced by the upstream :func:`tee` call. This "flattening step" | ||
allows nested :func:`tee` calls to share the same underlying data | ||
chain and to have a single update step rather than a chain of calls. | ||
|
||
.. note:: | ||
|
||
:func:`tee` automatically "flattens" existing tee objects, | ||
sharing the same underlying buffer instead of nesting them, to avoid | ||
performance degradation. This flattening behavior has existed since Python 3.7. | ||
|
||
.. versionchanged:: 3.13 | ||
Fixed a bug where re-teeing the first iterator did not correctly flatten | ||
the iterator chain in all cases. Previously, this could lead to unnecessary | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't see how that bug led to unnecessary nesting and performance degradation. I think you misunderstood it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. .. versionchanged:: 3.13 Is this description accurate, or have I misunderstood the nature of the bug? Thank you! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That description is accurate and you seem to have misunderstood it. Before jumping to a PR, I suggest to first wait for Trey to clarify what he means, and possibly for Raymond to comment on it. I suspect Trey is talking about that bug(fix) and Raymond didn't forget to add a note but intentionally didn't add one. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for pointing that out! I’ll pause work on this and wait for clarification . |
||
nesting and performance degradation in rare scenarios. | ||
|
||
.. doctest:: | ||
|
||
>>> it = iter([1, 2, 3]) | ||
>>> a, b = tee(it) | ||
>>> c, d = tee(a) | ||
>>> list(b) | ||
[1, 2, 3] | ||
>>> list(c) | ||
[1, 2, 3] | ||
>>> list(d) | ||
[1, 2, 3] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is the purpose of this? It has nothing to do with the bug and its fix, and seems to just redundantly show what the existing peeking example shows already. |
||
|
||
The flattening property makes tee iterators efficiently peekable: | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do you say since 3.7? It existed in Python 2 already.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Apologies for the oversight. Would it be fine if I remove this sentence and just describe the behavior without referencing a specific version history?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think so. It's described in the current documentation already.