Skip to main content

Python memoryview() Function

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>
Tags
Submitted by devanshi.srivastava on July 28, 2021

Devanshi, is working as a Data Scientist with iVagus. She has expertise in Python, NumPy, Pandas and other data science technologies.

About

At ProgramsBuzz, you can learn, share and grow with millions of techie around the world from different domain like Data Science, Software Development, QA and Digital Marketing. You can ask doubt and get the answer for your queries from our experts.