Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Python

memoryview()

Exposes an existing buffer through a view without copying its data.

Version: Python 3.14; repository image, production runtime not attested

Syntax

memoryview(object)

Exposes an existing buffer through a view without copying its data.

A writable view can modify the original byte array. A view over immutable bytes is read-only.

The view exposes storage supplied by the buffer protocol without copying it. For a bytearray, setting index 1 to the ASCII value 90 changes the original b to Z. Views over immutable bytes are read-only. Releasing a view, including by exiting its with block, ends access through it.

buf = bytearray(b"abc")
with memoryview(buf) as v:
    v[1] = 90
    assert v.obj is buf and bytes(buf) == b"aZc"
assert memoryview(object=b"abc").readonly

Examples

data = bytearray(b"abc")
view = memoryview(data)
view[1] = 90
print(bytes(data))
print(memoryview(b"abc").readonly)

Result

b'aZc'
True

The view exposes storage supplied by the buffer protocol without copying it. For a bytearray, setting index 1 to the ASCII value 90 changes the original b to Z. Views over immutable bytes are read-only. Releasing a view, including by exiting its with block, ends access through it.

Sources