class
ArrayViewArray view
Provides an untyped one-dimensional read-only view on a contiguous memory range. Convertible both to and from Python objects supporting the Buffer Protocol. The buffer type is lost in the conversion process and the memory is always treated as plain bytes:
>>> a = b'hello' >>> b = containers.ArrayView(a) >>> chr(b[2]) 'l' >>> bytes(b[1:4]) b'ell'
See the StridedArrayView1D and its multi-dimensional variants for an alternative that can provide typed access. The ArrayView is immutable, see MutableArrayView for the mutable alternative. All slicing operations are supported, specifying a non-trivial stride will return StridedArrayView1D instead of ArrayView.
Memory ownership and reference counting
Unlike in C++, the view keeps a reference to the original memory owner
object in the owner field, meaning that calling del
on the
original object will not invalidate the view. Slicing a view creates a
new view referencing the same original object, without any dependency on
the previous view. That means a long chained slicing operation will not
cause increased memory usage.
>>> b.owner is a True >>> b[1:4][:-1].owner is a True
The owner
is None
if the view is empty.
Comparison to Python’s memoryview
The ArrayView class is equivalent to one-dimensional
memoryview with a stride of 1
. For multiple dimensions and
non-trivial strides, StridedArrayView1D and friends provide a
superset of memoryview features.
Special methods
- def __buffer__(self, flags, /)
- Return a buffer object that exposes the underlying memory of the object.
- def __bytes__(self, /) -> bytes
- Convert to bytes
- def __getitem__(self, i: int) -> int
- Value at given position
- def __getitem__(self, slice: slice) -> object
- Slice the view
- def __init__(self, /) -> None
- Default constructor
- def __init__(self, arg0: Buffer, /) -> None
- Construct from a buffer
- def __len__(self, /) -> int
- View size
- def __release_buffer__(self, buffer, /)
- Release the buffer object that exposes the underlying memory of the object.