Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When should a memoryview be used? [duplicate]

The full description of memoryview can be found here:

Create a memoryview that references obj. obj must support the buffer protocol. Built-in objects that support the buffer protocol include bytes and bytearray.

A memoryview has the notion of an element, which is the atomic memory unit handled by the originating object obj. For many simple types such as bytes and bytearray, an element is a single byte, but other types such as array.array may have bigger elements.

like image 345
zr. Avatar asked Jan 30 '11 20:01

zr.


People also ask

When should a Memoryview be used?

A memoryview is essentially a generalized NumPy array structure in Python itself (without the math). It allows you to share memory between data-structures (things like PIL images, SQLlite data-bases, NumPy arrays, etc.) without first copying. This is very important for large data sets.

What is the use of Memoryview in Python?

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.

What is Bytes Bytearray Memoryview in Python?

bytearray uses integers for its values instead of bytes . A memoryview can also be used to wrap a bytearray . When you slice such a memoryview , the resulting object can be used to assign data to a particular portion of the underlying buffer.

What is buffer protocol in Python?

¶ The Python buffer protocol, also known in the community as PEP 3118, is a framework in which Python objects can expose raw byte arrays to other Python objects. This can be extremely useful for scientific computing, where we often use packages such as NumPy to efficiently store and manipulate large arrays of data.


2 Answers

A memoryview is essentially a generalized NumPy array structure in Python itself (without the math). It allows you to share memory between data-structures (things like PIL images, SQLlite data-bases, NumPy arrays, etc.) without first copying. This is very important for large data sets.

With it you can do things like memory-map to a very large file, slice a piece of that file and do calculations on that piece (easiest if you are using NumPy).

like image 197
Travis Oliphant Avatar answered Oct 10 '22 18:10

Travis Oliphant


From the documentation, I figure it's used to "access the internal data of an object that supports the buffer protocol without copying", so you can do things with huge chunks of data without filling up your memory. I don't know if you want examples, but I can't think of any, unfortunately.

like image 44
Jasmijn Avatar answered Oct 10 '22 16:10

Jasmijn