Python Glossary Русский

Python 3.14+Byte sequencesfunction

bytearray.resize()

resizing

Resizes a bytearray in place: extra bytes are truncated, new bytes are filled with null bytes. Returns None. Python 3.14+.

Syntax

bytearray.resize(size, /)

The method only exists in 3.14 and later — on 3.12/3.13 you get an AttributeError, and the portable equivalents are b += b'\x00' * n to grow and del b[n:] to shrink. Resizing may move the underlying buffer, so while a memoryview is exported on the bytearray the call fails with BufferError.

Examples

b = bytearray(b'abc')
print(b.resize(5))   # → None
print(b)             # → bytearray(b'abc\x00\x00')
print(len(b))        # → 5
b.resize(2)
print(b)             # → bytearray(b'ab')

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy