Built-in Functions and Types

memoryview()

Expose data provided through the buffer protocol without copying the underlying storage. A view can be read-only or writable depending on its exporter; slices can share storage. Item format, dimensions and strides determine access behavior, so not every view is a flat byte sequence. Release a view when finished (or use a context manager); subsequent operations on the released view generally fail. A writable view can modify its exporter and may prevent resizing while active.

Formal Syntax & Structure

python syntaxpython
memoryview(object, /)

Parameters & Attributes

NameTypeDescription
objectbuffer-protocol objectExporter such as bytes, bytearray or array.array.

Return Value

memoryview

A view retaining access to exported memory until released.

Working Example

Example Snippetpython
data = bytearray(b"ABC")with memoryview(data) as view:    view[1] = 90    print(view[1:3].tobytes())print(data.decode("ascii"))
Python Reference