On this page
memoryview
Prev: max | Up: __builtins__ | Next: min
Rel: bool, int, float, complex, str
Ref: docs.python.org — memoryview
Tags: #public #python3 #builtins
Import & Availability
# Built-in (available globally without import)
from builtins import memoryview
Overview & Signature
memoryview(object)
Create a new memoryview object which references the given object.
Common Methods & Attributes
memoryview.c_contiguous(...)— A bool indicating whether the memory is C contiguous.memoryview.cast(...)— Cast a memoryview to a new format or shape.memoryview.contiguous(...)— A bool indicating whether the memory is contiguous.memoryview.count(self, value, /)— Count the number of occurrences of a value.memoryview.f_contiguous(...)— A bool indicating whether the memory is Fortran contiguous.memoryview.format(...)— A string containing the format (in struct module style) for each element in the view.memoryview.hex(...)— Return the data in the buffer as a str of hexadecimal numbers.memoryview.index(self, value, start=0, stop=9223372036854775807, /)— Return the index of the first occurrence of a value.memoryview.itemsize(...)— The size in bytes of each element of the memoryview.memoryview.nbytes(...)— The amount of space in bytes that the array would use in a contiguous representation.memoryview.ndim(...)— An integer indicating how many dimensions of a multi-dimensional array the memory represents.memoryview.obj(...)— The underlying object of the memoryview.memoryview.readonly(...)— A bool indicating whether the memory is read only.memoryview.release(self, /)— Release the underlying buffer exposed by the memoryview object.memoryview.shape(...)— A tuple of ndim integers giving the shape of the memory as an N-dimensional array.memoryview.strides(...)— A tuple of ndim integers giving the size in bytes to access each element for each dimension of the array.memoryview.suboffsets(...)— A tuple of integers used internally for PIL-style arrays.memoryview.tobytes(self, /, order='C')— Return the data in the buffer as a byte string.memoryview.tolist(self, /)— Return the data in the buffer as a list of elements.memoryview.toreadonly(self, /)— Return a readonly version of the memoryview.
Usage
buf = bytearray(b"abcdef")
with memoryview(buf) as view:
sub = view[1:4] # zero-copy slice!
sub[0] = ord("B")
assert buf == b"aBcdef"
Gotchas
- Zero-copy slicing over
bytes/bytearray/array: Always release views (with memoryview(buf) as view:) before resizing abytearrayor closing anmmap.
REPL Inspect
help(memoryview)
Linked from
Related notes
- tuts/python3/__builtins__/__builtins__ Linked here; links to this page; shared tags: #builtins
- tuts/python3/__builtins__/max Linked here; links to this page; shared tags: #builtins
- tuts/python3/__builtins__/min Linked here; links to this page; shared tags: #builtins
- tuts/python3/__builtins__ links to this page; shared tags: #builtins
- tuts/python3/__builtins__/bool Linked here; shared tags: #builtins
- tuts/python3/__builtins__/complex Linked here; shared tags: #builtins
- tuts/python3/__builtins__/float Linked here; shared tags: #builtins
- tuts/python3/__builtins__/int Linked here; shared tags: #builtins
- tuts/python3/__builtins__/str Linked here; shared tags: #builtins
- tuts/python3/tutorials/advanced_python_internals/foreign_function_interfaces links to this page