Python memoryview() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Built-in type

Definition

In Python, memoryview(obj) creates a view of the memory inside a bytes-like object such as bytes, bytearray, or array.array. Slicing a view does not copy any data, so it is a zero-copy way to work with parts of large buffers.

A view of a bytearray is writable: changes made through the view appear in the original object. A view of immutable bytes is read-only. Call release(), or use the view in a with block, when you are finished, so the original object can be resized again.

memoryview is a built-in type (a class) that you call like a function: each call creates a new memoryview object. Like every built-in, it lives in the builtins module and is always available without an import.

Syntax

Wrap a bytes-like object, then index or slice the view.

python
view = memoryview(buffer)
part = view[2:6]          # no copy
data = part.tobytes()     # explicit copy
CallWhat it does
memoryview(b"abc")[0]Returns 97 (an int).
view[1:4]Another view sharing the same memory.
view.tobytes()Copies the viewed bytes into a new bytes object.
view.readonlyTrue for views of immutable objects.

Printing a view itself shows only a type name and memory address, so convert with bytes(view), tobytes(), or tolist() before printing.

Library declarations
python
memoryview(object)
# class memoryview: object must support the buffer protocol

Parameters

ParameterDescription
objectRequired. An object that supports the buffer protocol: bytes, bytearray, array.array, another memoryview, ...

Return Value

Return ValueDescription
memoryviewA view object that shares memory with object.

Python Examples

Example 1 — Read and Write Through a View

Index, slice, and modify a bytearray through a memoryview.

python
data = bytearray(b"abcdef")
view = memoryview(data)

print(view[0])
print(bytes(view[1:4]))
view[0] = ord("A")
print(data)
print(view.readonly, len(view))

How It Works

  1. Index returns an int. view[0] is 97, the byte value of a.
  2. Slice, then convert. view[1:4] is a smaller view; bytes() copies it for printing.
  3. Writes reach the original. Assigning through the view changes data itself to Abcdef.
  4. Writable view. A bytearray is mutable, so readonly is False; the view covers all 6 bytes.

Example 2 — Modify a Slice Without Copying

Overwrite the middle of a buffer through a sliced view, and compare with a real copy.

python
buffer = bytearray(b"0123456789")
middle = memoryview(buffer)[3:7]

middle[:] = b"WXYZ"
print(buffer)
print(middle.tobytes())
print(middle.nbytes)

copy = bytes(buffer[3:7])
buffer[3] = ord("!")
print(copy, middle.tobytes())

How It Works

  1. Sliced view. memoryview(buffer)[3:7] covers positions 3 to 6 without copying them.
  2. Slice assignment. middle[:] = b"WXYZ" writes four bytes straight into buffer.
  3. A copy is frozen. bytes(buffer[3:7]) copies the data, so it keeps WXYZ after the buffer changes.
  4. The view follows. After buffer[3] changes, the view reports !XYZ because it shares the memory.

Example 3 — Read-Only Views, release(), and BufferError

See why a view of bytes cannot be written, and why a bytearray cannot grow while a view is open.

python
raw = b"\x01\x02\x03\x04"
view = memoryview(raw)
print(view.readonly)
print(view.tolist())

try:
    view[0] = 9
except TypeError:
    print("a view of bytes is read-only")

data = bytearray(b"abc")
with memoryview(data) as locked:
    try:
        data.append(100)
    except BufferError:
        print("cannot resize while a view exists")
data.append(100)
print(data)

How It Works

  1. Immutable source. A view of bytes has readonly set to True; tolist() shows the four values.
  2. No writes. Assigning to a read-only view raises TypeError.
  3. Exports lock the size. Inside the with block a view exists, so append() raises BufferError.
  4. Released at the end. Leaving the with block calls release(), and the append then succeeds (100 is d).

Frequently Asked Questions

It lets you slice and modify large binary buffers without copying them, which saves memory and time when parsing files or network data.
No. A slice is another view of the same memory. Call bytes(view) or view.tobytes() when you need a copy.
No. str does not support the buffer protocol. Encode it first: memoryview(text.encode("utf-8")).
You tried to resize a bytearray while a memoryview of it exists. Release the view with view.release() or a with block first.
Convert it: print(view.tobytes()) or print(view.tolist()). Printing the view itself only shows its type and address.

Next: min()

Smallest item of an iterable or of several arguments.

min() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

7 people found this page helpful