The memoryview() function returns a memory view object of the given argument. It is used to get a memory view object from a specified object. It is the safe way to expose the buffer protocol in Python. It allows us to access the internal buffers of an object by creating a memory view object.
Syntax:
memoryview(obj)
Parameter Values:
- obj: It is used for internal data processing.
Return:
- Its give memoryview object of the given argument.
Buffer Protocol
It 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 and the other to access those buffers without intermediate copying.
It is only accessible to us at C-API and not using our normal code base.
Example:
byte_array = bytearray('abc', 'utf-8')
x = memoryview(byte_array)
print(x[0])
print(x[2:6])
print(x[1:3])
Output:
97 <memory at 0x7fc429536ac0> <memory at 0x7fc429536ac0>
Related Content
Tags