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.
SyntaxSyntax 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.
The Python memoryview() function returns a memoryview object of the specified object.
memoryview() Function ExamplesPractice the following examples to understand the use of memoryview() function in Python:
Example: Use of memoryview() FunctionThe 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: Byte Array to Memory View Using memoryview()
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: 84Example: Modifying Byte Array Using memoryview()
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: NumPy Array With memoryview() Function
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
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4