all Python 3Built-in functionsterm
memoryview
types
Creates a view into an object's buffer without copying the data. Lets you work with slices of binary data efficiently.
Syntax
memoryview(object)
memoryview only accepts objects that support the buffer protocol — bytes, bytearray, array.array; a str or a list raises TypeError. Writing through the view works only if the underlying object is mutable: assigning into memoryview(b'...') fails because bytes is read-only. Slicing a view gives another memoryview rather than bytes (hence the bytes(...) wrapper), and while a view is alive the bytearray cannot be resized — append or extend raise BufferError.
Examples
b = bytearray(b'hello world')
mv = memoryview(b)
print(bytes(mv[6:11])) # → b'world'
mv[0] = ord('H')
print(b) # → bytearray(b'Hello world')
print(mv.format) # → B (unsigned char)
print(mv.itemsize) # → 1
print(len(mv)) # → 11
See also
Open in the full glossarydocs.python.orgReport an inaccuracy