Skip to content

Commit 77ebe2c

Browse files
gh-145300: Document bytearray.extend() behavior when mutating during iteration
Add a note to sequence.extend() documentation clarifying that bytearray behaves differently from list and array.array when extending a sequence with itself during iteration. bytearray captures the length at the start and can at most double the original length, while list and array.array pick up elements as they're added, potentially leading to unbounded growth.
1 parent 149c465 commit 77ebe2c

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

Doc/library/stdtypes.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1328,6 +1328,17 @@ Mutable sequence types also support the following methods:
13281328
For the most part, this is the same as writing
13291329
``seq[len(seq):len(seq)] = iterable``.
13301330

1331+
.. note::
1332+
1333+
When extending a sequence with itself (e.g., ``seq.extend(seq)``),
1334+
the behavior differs between sequence types if the sequence mutates
1335+
during iteration:
1336+
1337+
* :class:`list` and :class:`array.array` pick up elements as they
1338+
are added, potentially leading to unbounded growth.
1339+
* :class:`bytearray` captures the sequence length at the start,
1340+
limiting the extension to at most doubling the original length.
1341+
13311342
.. method:: bytearray.insert(index, value, /)
13321343
list.insert(index, value, /)
13331344
:no-contents-entry:

0 commit comments

Comments
 (0)