Why we use memoryview() function?
As Memory view is a safe way to expose the buffer protocol in Python and a memoryview behaves just like bytes in many useful contexts (for example, it supports the mapping protocol) so it provides an adequate replacement if used carefully. The great thing about it is that it uses the buffer protocol beneath the covers to avoid copies and just juggle pointers to data.
So before we get into what memory views, we need to first understand about Buffer Protocol.
Buffer Protocol : Buffer protocol provides a way to access the internal data of an object. This internal data is a memory array or a buffer. It allows one object to expose its internal data (buffers) and the other to access those buffers without intermediate copying.
Buffer protocol is only accessible to us at the C-API level and not using our normal code base.
So, to expose the same protocol to normal Python code base, memory views are present.
Memory view : memoryview objects allow Python code to access the internal data of an object that supports the buffer protocol without copying.
The memoryview() function allows direct read and write access to an object’s byte-oriented data without needing to copy it first. That can yield large performance gains when operating on large objects since it doesn’t create a copy when slicing.
memoryview(obj) Parameters: obj - object whose internal data is to be exposed. supporting buffer protocol - str and bytearray (but not unicode). Return Value: Returns a memoryview object.
Before update: bytearray(b'XYZ') After update: bytearray(b'XYJ')
Explanation how we modify internal data in above program : Here, we updated the memory view’s 2nd index to ASCII value as 74 (J). In this memory view object mem_view references the same buffer or memory and updating the index in mem_view and it also updates byte_array.
Importance of buffer protocol and memory views
By using buffer protocol we can work on large data like we want to work on binary data of an image. Buffer protocol, can create another object access to modify the large data without copying it. This makes the program use less memory and increases the execution speed.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
- Important differences between Python 2.x and Python 3.x with examples
- Python | Set 4 (Dictionary, Keywords in Python)
- Python | Sort Python Dictionaries by Key or Value
- Python | Merge Python key values to list
- Reading Python File-Like Objects from C | Python
- Python | Add Logging to a Python Script
- Python | Add Logging to Python Libraries
- Python | Visualizing O(n) using Python
- Python | Index of Non-Zero elements in Python list
- Python | Convert list to Python array
- MySQL-Connector-Python module in Python
- Python - Read blob object in python using wand library
- Python | PRAW - Python Reddit API Wrapper
- twitter-text-python (ttp) module - Python
- Reusable piece of python functionality for wrapping arbitrary blocks of code : Python Context Managers
- Python program to check if the list contains three consecutive common numbers in Python
- Creating and updating PowerPoint Presentations in Python using python - pptx
- How to write an empty function in Python - pass statement?
- Operator Functions in Python | Set 2
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.