memoryview()
memoryview(object) Returns:
memoryview · Added in v3.0 · Updated March 13, 2026 · Built-in Functions buffer bytes memory built-in
The memoryview() function returns a memory view object that exposes the underlying buffer of a bytes or bytearray object without making a copy. This is particularly useful for working with large binary data efficiently, as it allows you to read and modify the data in-place without the overhead of copying.
Syntax
memoryview(object)
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
object | bytes, bytearray, or object implementing the buffer protocol | Required | The object to create a memory view from. Must support the buffer protocol (e.g., bytes, bytearray, numpy arrays). |
Examples
Basic usage
# Create a memory view from bytes
data = b'Hello, World!'
view = memoryview(data)
print(view[:5]) # b'Hello'
print(view[7:]) # b'World!'
print(list(view)) # [72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33]
Modifying data with bytearray
# Create a memory view from bytearray (mutable)
data = bytearray(b'Hello')
view = memoryview(data)
# Modify in-place (only works with mutable objects)
view[0] = 74 # 'J' (ASCII 74)
print(data) # bytearray(b'Jello')
print(view[:]) # memoryview(b'Jello')
Working with large data efficiently
# Efficient slice operations without copying
large_data = b'x' * 1000000
view = memoryview(large_data)
# This doesn't copy - it's a view into the same memory
slice_view = view[100:200]
print(len(slice_view)) # 100
Using C-style format strings
# Convert to different data types using format
import struct
data = struct.pack('iif', 42, 100, 3.14)
view = memoryview(data)
# Read as integers and float
int_view = view.cast('i') # 4-byte unsigned integers
float_view = view.cast('f') # 4-byte floats
print(int_view[0]) # 42
print(int_view[1]) # 100
print(float_view[2]) # 3.140000104904175
Common Patterns
Zero-copy slicing
Memory views allow you to slice and dice data without copying:
data = b'0123456789'
view = memoryview(data)
# All these are zero-copy views
first_half = view[:5] # b'01234'
second_half = view[5:] # b'56789'
step_view = view[::2] # b'02468' (every other byte)
Comparing memory views
a = b'Hello'
b = b'Hello'
print(memoryview(a) == memoryview(b)) # True
print(bytes(memoryview(a)) == b) # True
Working with numpy arrays
# memoryview works with any buffer-protocol object
import numpy as np
arr = np.array([1, 2, 3, 4, 5], dtype=np.uint8)
view = memoryview(arr)
print(view[:3]) # b'\x01\x02\x03'
Errors
-
TypeError: If the object doesn’t support the buffer protocol:
memoryview("string") # TypeError: cannot use a string as source for memoryview -
TypeError: If trying to modify a read-only buffer:
data = b'readonly' view = memoryview(data) view[0] = 65 # TypeError: cannot modify read-only memory -
ValueError: If the format is not supported for casting:
view = memoryview(b'abc') view.cast('Q') # ValueError: memoryview: cannot cast from bytes to unsigned long long
See Also
- bytearray::bytearray — Mutable sequence of bytes
- bytes::bytes — Immutable sequence of bytes
- struct — Pack and unpack binary data