Python memoryview() Function



The Python memoryview() function is a built-in function used to obtain the "memory view" object from a specified object. This object allows us to access the internal data of any other object without copying the data. But the only condition is that the given object must support the buffer protocol.

When we are working with large data sets, instead of copying that data, which can consume more resources, the memoryview() function operates on the existing data, which helps in improving efficiency by saving both time and memory.

Syntax

Syntax of the Python memoryview() function is shown below −

memoryview(object)

Parameters

The Python memoryview() function accepts a single parameter −

  • object − This parameter represents a bytes object or a bytearray object.

Return Value

The Python memoryview() function returns a memoryview object of the specified object.

Examples

Now, let's understand the working memoryview() function with the help of some examples −

Example 1

The memoryview() function is applicable to objects that support the buffer protocol. When an inappropriate object is passed to this function, it will return "TypeError". In the code below, we are passing a list which doesn't support buffer protocol. Hence, we will encounter TypeError.

numericList = [29, 28, 27, 26, 25]
memView = memoryview(numericList)
print("The memory view of given list:")
print(memView) 

Following is an output of the above code −

TypeError: memoryview: a bytes-like object is required, not 'list'

Example 2

In this example, we are using the Python memoryview() function to convert a Bytearray to a Memoryview object. We then display the first element of that object.

byteArray = bytearray("TUTORIALSPOINT", "utf-8")
mmviewOfArr = memoryview(byteArray)
print("Accessing the first element of memoryview:")
print(mmviewOfArr[0])

When we run above program, it produces following result −

Accessing the first element of memoryview:
84

Example 3

We can also modify a given byte array through the memoryview() function. In the code below, a bytearray is defined and by using its indices, we are modifying values of the first three characters.

byteArray = bytearray("TUTORIALSPOINT", "utf-8")
mmviewOfArr = memoryview(byteArray)
print("Orginal element of memoryview:", byteArray)
mmviewOfArr[0] = 116
mmviewOfArr[1] = 117
mmviewOfArr[2] = 116
print("After modifying the memoryview:", byteArray)

Following is an output of the above code −

Orginal element of memoryview: bytearray(b'TUTORIALSPOINT')
After modifying the memoryview: bytearray(b'tutORIALSPOINT')

Example 4

Since the numpy supports the buffer protocol, it is compatible with the memoryview() function. In the following Python code, we have created a numpy array and displaying the last three elements of that array.

import numpy as np
newnpArr = np.array([29, 28, 27, 26, 25])
memView = memoryview(newnpArr)
print("The last three elements of array:")
print(memView[2:5].tolist())  

Output of the above code is as follows −

The last three elements of array:
[27, 26, 25]
python_built_in_functions.htm
Advertisements