10. memoryview type¶
New in version 2.7.
memoryview
objects allow Python code to access the internal data
of an object that supports the buffer protocol without copying. Memory
is generally interpreted as simple bytes.
-
class
memoryview
(obj)¶ Create a
memoryview
that references obj. obj must support the buffer protocol. Built-in objects that support the buffer protocol includestr
andbytearray
(but notunicode
).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 asstr
andbytearray
, an element is a single byte, but other third-party types may expose larger elements.len(view)
returns the total number of elements in the memoryview, view. Theitemsize
attribute will give you the number of bytes in a single element.A
memoryview
supports slicing to expose its data. Taking a single index will return a single element as astr
object. Full slicing will result in a subview:>>> v = memoryview('abcefg') >>> v[1] 'b' >>> v[-1] 'g' >>> v[1:4] <memory at 0x77ab28> >>> v[1:4].tobytes() 'bce'
If the object the memoryview is over supports changing its data, the memoryview supports slice assignment:
>>> data = bytearray('abcefg') >>> v = memoryview(data) >>> v.readonly False >>> v[0] = 'z' >>> data bytearray(b'zbcefg') >>> v[1:4] = '123' >>> data bytearray(b'z123fg') >>> v[2] = 'spam' Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: cannot modify size of memoryview object
Notice how the size of the memoryview object cannot be changed.
memoryview
has two methods:-
tobytes
()¶ Return the data in the buffer as a bytestring (an object of class
str
).>>> m = memoryview("abc") >>> m.tobytes() 'abc'
-
tolist
()¶ Return the data in the buffer as a list of integers.
>>> memoryview("abc").tolist() [97, 98, 99]
There are also several readonly attributes available:
-
format
¶ A string containing the format (in
struct
module style) for each element in the view. This defaults to'B'
, a simple bytestring.
-
itemsize
¶ The size in bytes of each element of the memoryview.
-
shape
¶ A tuple of integers the length of
ndim
giving the shape of the memory as an N-dimensional array.
-
ndim
¶ An integer indicating how many dimensions of a multi-dimensional array the memory represents.
-
strides
¶ A tuple of integers the length of
ndim
giving the size in bytes to access each element for each dimension of the array.
-
readonly
¶ A bool indicating whether the memory is read only.
-