Methods and Functions

Python memoryview()

You must have rarely heard about the Python memoryview() function. What exactly is it? Why does Python have such a function? The memoryview() built-in function is been used in very specific applications and also helps improve performance. The memoryview() objects enable Python to directly read and write access to the internal byte-oriented data of an object that supports the buffer protocol without actually copying it. So, what is this Buffer Protocol? Let us understand this terminology before moving to the Python memoryview() function.

Python Buffer Protocol

The buffer protocol allows you to access an object’s internal data. This internal data can consist of a memory array or a buffer. It is a Python protocol that allows you to talk directly to a data structure and retrieve its data.

The buffer protocol enables one object to reveal its internal data (buffers) and the other to access those buffers without the need for intermediate copying. In high-level Python, dealing with the buffer is faster than working with an object.

At the C level, this protocol is been implemented. The buffer protocol is only available to us at the C-API level, not through our standard codebase. As a result, memory views are present in order to provide the same protocol to the standard Python codebase.

Python memoryview() Definition

  • The Python memoryview() function returns a memory view object based on the argument.
  • Python memoryview() objects enable Python code to access the internal data of an object that implements the buffer protocol without the need for copying.

What is a memory view?

In Python, a memory view is a secure technique to expose the buffer protocol. It allows you to access an object’s internal buffers by constructing a memory view object.

The memoryview() method in Python returns a memoryview object based on the bytes or bytearray parameter. It allows you to access the data without having to copy/replicate it first. Here without copying means that we will obtain the reference to the data rather than the data itself. Because it does not generate a copy while slicing, this can result in significant speed benefits when working with huge objects.

Why Buffer Protocol and memory views are important?

Python memoryview’s are handy because, unlike bytes or strings, they can be sliced without copying the raw source data. As a result, the application consumes less memory and executes faster. How is that possible? Allow us to comprehend.

Python must produce a copy of an object whenever we perform an action on it (for example, calling a function of an object or slicing an array). If we have a large chunk of data to deal with (for example, binary data from an image), we would produce copies of massive portions of data that would be essentially useless. When you need portions of binary data that simply need to allow indexing, memoryview objects are ideal.

Python memoryview() Syntax

We use the following syntax:



memoryview() Parameters

Python memoryview() function accepts a single parameter:

obj = The internal data to be exposed. Can either be bytes or bytearray object.  It must, however, implement the buffer protocol.

Return value from memoryview()

 The Python memoryview() function returns the memory view object of the given argument.

Example 1: How memoryview() works in Python?

Let’s get straight to the examples and understand how the memoryview() function works by looking at this simple Python program.



# Python program to illustrate memoryview()
my_byte_array = bytearray('ABCDE', 'utf-8')

# Create memoryview object
mv = memoryview(my_byte_array)

print(type(mv))   # displays the type of variable mv
print(mv)  # displays memory location of the created memoryview object

# access memory view's index

# create byte from memory view

# create list from memory view



<class 'memoryview'>

[65, 66, 67, 68, 69]

We’ve made a memory view object ‘mv’ out of the byte array ‘my_byte_array’. Then we printed the mv’s 0th and 2nd indexes, ‘A’ and ‘C’ (which gives the ASCII value – 65 and 67 respectively). We then took the mv’s indices from 0 to 2, ‘ABC’, and converted them to bytes. Finally, we retrieved all of mv’s indices and turned them into a list. Because bytearray internally holds ASCII values for the alphabets, the output is a list of A, B, C, D, and E ASCII values.

Example 2: Modify internal data using memory view

What happens if we try to change the memoryview object? Will it also change the original value? Let’s have a look:



# Python program to illustrate memoryview()
my_byte_array = bytearray('ABCDEF', 'utf-8')
print('Before updation:', my_byte_array)

# Create memoryview object
mv = memoryview(my_byte_array)

# update 3rd index of mv to X
mv[3] = 88
# update 4th index of mv to Y
mv[4] = 89
# update 5th index of mv to Z
mv[5] = 90

print('After updation:', my_byte_array)



Before updation: bytearray(b'ABCDEF')
After updation: bytearray(b'ABCXYZ')

In the above program, we have updated the memory view ‘mv’ 3rd, 4th and 5th index to 88, 89, and 90, which corresponds to the ASCII values of X, Y, and Z respectively. Because the memory view object ‘mv’ refers to the same buffer/memory as ‘my_byte_array’, altering the index in mv also updates my_byte_array.

Share with friends

Customize your course in 30 seconds

Which class are you in?
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
Ashhar Firdausi
IIT Roorkee
Dr. Nazma Shaik
Gaurav Tiwari
Get Started

Leave a Reply

Your email address will not be published. Required fields are marked *

Download the App

Watch lectures, practise questions and take tests on the go.

Customize your course in 30 seconds

No thanks.