Skip to main content

Python memoryview() Function

The memoryview() function returns a memory view object from a specified object.

Syntax

memoryview(obj)

memoryview() Parameters

Python memoryview() function parameters:

ParameterConditionDescription
objRequiredThe object whose internal data is to be exposed. Must support the buffer protocol, such as str and bytearray (but not unicode)

memoryview() Return Value

Python memoryview() function returns a memory view object.

Examples

Example 1: Basic usage of memoryview

This example shows how to use memoryview for efficient byte manipulation in Python, using a bytearray created from a string. It accesses and converts bytes without creating a copy.

byte_array = bytearray('XYZ', 'utf-8')
mv = memoryview(byte_array)
print(mv[0]) # Accessing the first byte
print(bytes(mv[0:1])) # Converting the first byte to bytes

output

88
b'X'

Example 2: Modifying internal data using memoryview

This example shows how to modify a bytearray using a memoryview. It changes the third byte of the bytearray from Z to J, as shown by the output before and after the update. The change is done directly on the bytearray without creating a copy, showing the efficiency of memoryview for byte manipulation.

byte_array = bytearray('XYZ', 'utf-8')
print('Before update:', byte_array)
mem_view = memoryview(byte_array)
mem_view[2] = 74 # ASCII value for 'J'
print('After update:', byte_array)

output

Before update: bytearray(b'XYZ')
After update: bytearray(b'XYJ')

Example 3: Converting memoryview to bytes

This example shows how to convert a memoryview object back into a bytes object. It creates a bytearray from a string, makes a memoryview of it, and then converts that memoryview back into a bytes object.

The type of the resulting object is printed, confirming it is a bytes object.

byte_array = bytearray('XYZ', 'utf-8')
mem_view = memoryview(byte_array)
byt = bytes(mem_view)
print(type(byt)) # Output: <class 'bytes'>

output

<class 'bytes'>

Example 4: Converting memoryview to string

This example shows how to convert a memoryview object into a str (string) object. It creates a bytearray from a string, makes a memoryview of it, and then converts that memoryview into a str object.

The type of the resulting object is printed, confirming it is a str object. However, note that this does not give a human-readable string, but rather a string representation of the memoryview object.

byte_array = bytearray('XYZ', 'utf-8')
mem_view = memoryview(byte_array)
string = str(mem_view)
print(type(string)) # Output: <class 'str'>

output

<class 'str'>

Example 5: Using memoryview with NumPy Arrays

This example shows how to use a memoryview to directly modify a numpy array. It changes the third element of the numpy array from 3 to 10, as shown by the output. The change is done directly on the numpy array without creating a copy, showing the efficiency of memoryview for data manipulation.

import numpy as np
numpy_array = np.array([1, 2, 3, 4, 5])
mv_numpy = memoryview(numpy_array)
mv_numpy[2] = 10
print(numpy_array) # Output: [1, 2, 10, 4, 5]

output

[ 1  2 10  4  5]

Some details

What is a memory view?

A memory view in Python is a secure method to access the buffer protocol.

It enables access to the internal buffers of an object by generating a memory view object.

Why buffer protocol and memory view are important?

We need to remember that whenever we perform some action on an object (call a function of an object, slice an array), Python needs to create a copy of the object.

If we have large data to work with (e.g. binary data of an image), we would unnecessarily create copies of huge chunks of data, which serves almost no use.

Using the buffer protocol, we can give another object access to use/modify the large data without copying it. This makes the program use less memory and increases the execution speed.