When should a memoryview be used? [duplicate]
Asked Answered
N

2

39

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.

Nowell answered 30/1, 2011 at 20:44 Comment(1)
I was asking the same from google, just found something relevantUnperforated
R
34

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).

Roscoeroscommon answered 17/4, 2013 at 4:22 Comment(0)
Y
3

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.

Yeast answered 30/1, 2011 at 20:52 Comment(1)
Thanks, but i read that too in the doc, which wasn't very informative.Nowell

© 2022 - 2024 — McMap. All rights reserved.