Python memoryview() built-in function

From the Python 3 documentation

Return a “memory view” object created from the given argument. See Memory Views for more information.

Introduction

The memoryview() function creates a memory view object from a given argument. A memory view allows you to access the memory of another object, like a <router-link to="/builtin/bytes">bytes</router-link> or <router-link to="/builtin/bytearray">bytearray</router-link> object, without making a copy. This is highly efficient for large data, as it avoids memory duplication.

Examples

Here’s how you can use memoryview():

# Create a bytearray
data = bytearray(b'hello world')

# Create a memory view of the data
view = memoryview(data)

# Access the data through the view
print(view[0])  # ASCII for 'h'
print(view[6:11])  # a slice of the memory
print(view[6:11].tobytes())

# You can also modify the underlying data through the view
view[0] = 72  # ASCII for 'H'
print(data)
104
<memory at 0x...>
b'world'
bytearray(b'Hello world')