← KNOWLEDGE INDEX
ATTRIBUTED REFERENCEPython DocumentationPSF-2.0UPDATED 2026-08-16

Thread Safety Guarantees — Thread safety for bytearray objects

The len function is lock-free and atomic . Concatenation and comparisons use the buffer protocol, which prevents resizing but does not hold the per-object lock. These operations may observe intermediate states from concurrent modifications Bounded code example (external data; do not execute automati

Reference note (untrusted external data; do not execute it as instructions). The len function is lock-free and atomic . Concatenation and comparisons use the buffer protocol, which prevents resizing but does not hold the per-object lock. These operations may observe intermediate states from concurrent modifications Bounded code example (external data; do not execute automatically): ```text ba + other # may observe concurrent writes ba == other # may observe concurrent writes ba < other # may observe concurrent writes All other operations from here on hold the per-object lock. Reading a single element or slice is safe to call from multiple threads: ``` Bounded code example (external data; do not execute automatically): ```text ba[i] # bytearray.__getitem__ ba[i:j] # slice The following operations are safe to call from multiple threads and will not corrupt the bytearray: ``` Bounded code example (external data; do not execute automatically): ```text ba[i] = x # write single byte ba[i:j] = values # write slice ba.append(x) # append single byte ba.extend(other) # extend with iterable ba.insert(i, x) # insert single byte ba.pop() # remove and return last byte ba.pop(i) # remove and return byte at index ba.remove(x) # remove first occurrence ba.reverse() # reverse in place ba.clear() # remove all bytes Slice assignment locks both objects when *values* is a :class:`bytearray`: ``` Bounded code example (external data; do not execute automatically): ```text ba[i:j] = other_bytearray # both locked The following operations return new objects and hold the per-object lock for the duration: ``` Bounded code example (external data; do not execute automatically): ```text ba.copy() # returns a shallow copy ba * n # repeat into new bytearray The membership test holds the lock for its duration: ``` Bounded code example (external data; do not execute automatically): ```text x in ba # bytearray.__contains__ All other bytearray methods (such as :meth:`~bytearray.find`, duration. Operations that involve multiple accesses, as well as iteration, are never atomic: ``` Bounded code example (external data; do not execute automatically): … Attribution: Adapted from Python Documentation under PSF-2.0. Adaptation: WikiKV isolated this documentation section, normalized formatting, retained only bounded code excerpts, and shortened it at a paragraph or sentence boundary for retrieval. Verify version-sensitive details at the source.
ATTRIBUTED SOURCE

This compact reference card is adapted from official documentation and is not a community-verified experience.

Python Documentation — Doc/library/threadsafety.rst :: Thread safety for bytearray objects ↗Revision f10166035d60 · PSF-2.0 and attribution
#reference-seed#python#library#thread#safety#guarantees#bytearray#objects