Python Reference Library →

Python memoryview() Function – Work With Binary Data Efficiently

Let’s look at memoryview() function – work with binary data efficiently

  1. Notice

    memoryview() exposes a supported buffer without copying its contents. A writable view can change the underlying mutable buffer.

  2. Try

    Follow the example, change its sample, and compare what happens.

  3. Check

    Explain the result and test your understanding.

Watch how it works

Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.

Explore the idea

Follow the code and watch its state change. Guided simulation.

Code
Python
data = bytearray([3, 66])
view = memoryview(data)
view[0] = 65
print(list(data))
Live state

Top-level scope

data→Object 1
view→Object 2
Object 1 · bytearraybytearray(b'AB')Object 2 · memoryview<memory>
This step

memoryview() exposes a supported buffer without copying its contents. A writable view can change the underlying mutable buffer.

Picture memoryview() function – work with binary data efficiently

memoryview() exposes a supported buffer without copying its contents. A writable view can change the underlying mutable buffer.

Image brief

Alt text
A diagram for Python memoryview() Function – Work With Binary Data Efficiently. Instructions: data = bytearray([3, 66]) view = memoryview(data) view[0] = 65 print(list(data)). Displayed result: [65, 66]. The final example bindings are data --> object 1 (bytearray); view --> object 2 (memoryview). object 1: bytearray(b'AB'); object 2: <memory>
Image generation prompt
Create a clean educational illustration for a Python programming lesson titled "Python memoryview() Function – Work With Binary Data Efficiently". Teaching focus: memoryview() exposes a supported buffer without copying its contents. A writable view can change the underlying mutable buffer. Depict exactly the relationships and labels in this reference diagram: EXAMPLE INSTRUCTIONS data = bytearray([3, 66]) view = memoryview(data) view[0] = 65 print(list(data)) | v DISPLAYED RESULT [65, 66] FINAL BINDINGS data --> object 1 (bytearray) view --> object 2 (memoryview) object 1: bytearray(b'AB') object 2: <memory> Use a flat vector style, generous spacing, clear arrows and legible labels, with a restrained blue/teal palette on a plain background. The diagram must explain the programming idea accurately. Do not depict variables as boxes containing copied objects when they share a reference. Avoid mascots, decorative screenshots, logos, gradients and extra text. Landscape composition, approximately 3:2, suitable for a course card. Accessible description: A diagram for Python memoryview() Function – Work With Binary Data Efficiently. Instructions: data = bytearray([3, 66]) view = memoryview(data) view[0] = 65 print(list(data)). Displayed result: [65, 66]. The final example bindings are data --> object 1 (bytearray); view --> object 2 (memoryview). object 1: bytearray(b'AB'); object 2: <memory>

Follow the execution

Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.

Execution workspace

Choose a sample and follow its execution. Guided simulation.

Code
Python
data = bytearray([3, 66])
view = memoryview(data)
view[0] = 65
print(list(data))
Live state

Top-level scope

data→Object 1
view→Object 2
Object 1 · bytearraybytearray(b'AB')Object 2 · memoryview<memory>
This step

The example has finished. Compare the final output with the values and references above.

Terminal
Output
[65, 66]

What do you expect this program to print?

What does this example display?

data = bytearray([3, 66])
view = memoryview(data)
view[0] = 65
print(list(data))
What does this example display?
Choose one answer. You can try again.

Answer and explanation

[65, 66]

Follow the printed values in order. The output is: [65, 66]

Which explanation matches what you learned?

Which explanation matches what you learned?
Choose one answer. You can try again.

Answer and explanation

memoryview() exposes a supported buffer without copying its contents. A writable view can change the underlying mutable buffer.

memoryview() exposes a supported buffer without copying its contents. A writable view can change the underlying mutable buffer.

More examples and reference

Python memoryview() Function

The memoryview() function in Python provides a way to access the memory of a binary object (like bytes or bytearray) without copying the actual data. It's very useful for working with large data buffers efficiently.

Syntax

memoryview(obj)

Parameters:

  • obj – A binary object that supports the buffer protocol, like bytes, bytearray, or array.array.

Returns:

  • A memoryview object that references the same data without copying it.

Example: Using memoryview() with bytearray

data = bytearray("hello", "utf-8")
mv = memoryview(data)

print(mv[0])        # Output: 104 (ASCII of 'h')
mv[0] = 72          # Change 'h' to 'H'
print(data)         # Output: bytearray(b'Hello')
104
bytearray(b'Hello')

Why Use memoryview()?

  • Efficient: No need to copy data
  • Fast: Great for performance with large datasets
  • Direct access: You can slice, read, or modify without unpacking

Example: Slicing Memory Efficiently

numbers = bytearray([10, 20, 30, 40, 50])
mv = memoryview(numbers)
slice_mv = mv[1:4]

print(list(slice_mv))
[20, 30, 40]

Converting Back to Bytes

mv = memoryview(b"data")
print(bytes(mv))
b'data'

Common Mistakes

  • Using memoryview() on non-buffer objects like strings → will raise TypeError
  • Trying to assign new values to a memoryview of bytes (which is immutable)

Interview Tip

In performance-sensitive applications (like image processing, network packets, or binary file handling), memoryview() helps reduce memory overhead.

Summary

  • memoryview() gives you access to the internal data of a binary object.
  • It is efficient because it avoids data copying.
  • Supports slicing and modification (if the source is mutable).

Practice Problem

Create a bytearray from the word "data", convert it to a memoryview, change 'd' to 'D', and print the updated bytearray.

b = bytearray("data", "utf-8")
mv = memoryview(b)
mv[0] = ord("D")
print(b)

Expected Output:

bytearray(b'Data')

What to remember

Connect what you noticed in the example with the idea below.

Remember

memoryview() exposes a supported buffer without copying its contents. A writable view can change the underlying mutable buffer.

Watch out for this idea

A memoryview always makes an independent copy of the buffer.

This is the misconception to avoid.

Reference checks are optional and do not count toward course progress.

    Reference library →