The memoryview () function in Python
Before understanding what a memory view is, we need to talk a little bit about Python's protocol buffers.
What is Python's buffer protocol?
The buffer protocol provides a way to access the internal data of an object. This internal data can be a memory array or buffer.
The buffer protocol allows one object to expose data (buffer) inside it and another object can access those buffers without the need for intermediate replication.
This protocol is only accessible to data at C-API level and does not use the normal code base. Therefore, to display data in the same protocol as a regular Python code base, we need a memory view.
What is memory view?
Memory view is a safe way to display buffer protocols in Python. It allows you to access the buffer inside an object by creating a memory view object.
Why are buffer protocol and memory view so important in Python?
Whenever we perform actions on an object (executing a function on an object, cutting an array .), Python creates a copy of the object. This will consume memory, slowing down processing if we work with large amounts of data.
memoryview () is an important function of PythonBy using the buffer protocol, we can allow an access object to use / modify data without having to copy an extra copy. This will help the program use less memory and speed up the code processing.
The memoryview function syntax ()
To display the protocol buffer using memoryview () , we use the following syntax:
memoryview()
Memoryview () function parameter
The memoryview () function has only one parameter:
- Object: The object containing the data you want to use memoryview () to access. This object must support protocol buffers (bytes, bytearray).
The value returned from the memoryview () function
The memoryview () function returns the memory view of the object.
Example 1: How does memoryview () function work in Python?
#random bytearray random_byte_array = bytearray('TIPSMAKE', 'utf-8') mv = memoryview(random_byte_array) # Access index at position 0 of memory view print(mv[0]) # Create byte from memory view print(bytes(mv[0:2])) # create list memory view print(list(mv[0:3]))
When running the program, the result returned is:
81 b'QU' [81, 85, 65, 78, 84, 82, 73, 77, 65, 78, 71]
Here, we create a mv memory view from the random_byte_array byte array .
Then we access the index at position 0 of mv, the value of this index is Q. This index is printed according to the ASCII value of 81.
Next, we access the 0 and 1 position indexes of mv , QU, and convert them into bytes. Finally, we access all of the mv 's indexes and convert them into an ASCII list.
Example 2: Editing data with memoryview ()
# random bytearray random_byte_array = bytearray('QTIPSMAKE', 'utf-8') print('before fixing:', random_byte_array) mv = memoryview(random_byte_array) # Update the index of the number 1 position of mv into U mv[1] = 85 print('after fixing:', random_byte_array)
When running the program, the result is:
before fixing: bytearray(b'QTIPSMAKE') after: bytearray(b'TIPSMAKE')
Here, we update the number 1 position index of mv from V to U, 85 being U.'s ASCII code.
Because the memory view mv object references the same buffer / memory, updating the index in mv also updates random_byte_array .
You should read it
- The ord () function in Python
- Max () function in Python
- Int () function in Python
- The function id () in Python
- Zip () function in Python
- Hex () function in Python
- The oct () function in Python
- The function dir () in Python
- The map () function in Python
- The next () function in Python
- The function set () in Python
- Help () function in Python
Maybe you are interested
The world's 'stupid' bird, at the moment of life and death it 'loses its memory'
Nvidia launches RTX 4070 version with 'super fast' GDDR6 memory
Nvidia is developing a line of AI GPUs with 144GB HBM3E memory
3 Ways to free up Google Drive space, increase memory
Samsung begins mass production of 'most advanced' 12nm DDR5 memory chips
Samsung's HBM3E memory failed the evaluation test set by NVIDIA