Memoryview Methods

memoryview.toreadonly()

Create a new view that rejects writes through that view while retaining shared storage. The source view keeps its original access permissions; another writable alias can still change what the read-only view sees. This is not an immutable snapshot or a mechanism for freezing the exporter. Release each live view when finished.

Formal Syntax & Structure

python syntaxpython
view.toreadonly()

Return Value

memoryview

New read-only view of shared storage.

Working Example

Example Snippetpython
data = bytearray(b"ABC")with memoryview(data) as writable:    with writable.toreadonly() as readonly:        print(writable.readonly, readonly.readonly)        writable[0] = 90        print(readonly.tobytes())
Python Reference