Remember
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.
Follow the example, change its sample, and compare what happens.
Explain the result and test your understanding.
Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.
Follow the code and watch its state change. Guided simulation.
data = bytearray([3, 66])
view = memoryview(data)
view[0] = 65
print(list(data))Top-level scope
bytearray(b'AB')Object 2 · memoryview<memory>memoryview() exposes a supported buffer without copying its contents. A writable view can change the underlying mutable buffer.
(no output)
What do you expect this program to print?
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>Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.
Choose a sample and follow its execution. Guided simulation.
data = bytearray([3, 66])
view = memoryview(data)
view[0] = 65
print(list(data))Top-level scope
bytearray(b'AB')Object 2 · memoryview<memory>The example has finished. Compare the final output with the values and references above.
[65, 66]
What do you expect this program to print?
data = bytearray([3, 66])
view = memoryview(data)
view[0] = 65
print(list(data))memoryview() FunctionThe 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.
memoryview(obj)
obj – A binary object that supports the buffer protocol, like bytes, bytearray, or array.array.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')
numbers = bytearray([10, 20, 30, 40, 50])
mv = memoryview(numbers)
slice_mv = mv[1:4]
print(list(slice_mv))
[20, 30, 40]
mv = memoryview(b"data")
print(bytes(mv))
b'data'
memoryview() on non-buffer objects like strings → will raise TypeErrormemoryview of bytes (which is immutable)In performance-sensitive applications (like image processing, network packets, or binary file handling), memoryview() helps reduce memory overhead.
memoryview() gives you access to the internal data of a binary object.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)
bytearray(b'Data')
Connect what you noticed in the example with the idea below.
memoryview() exposes a supported buffer without copying its contents. A writable view can change the underlying mutable buffer.
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.