IMPORTANT: To view this page as Markdown, append `.md` to the URL (e.g. /docs/manual/basics.md). For the complete Mojo documentation index, see llms.txt.
Skip to main content
Version: Nightly
For the complete Mojo documentation index, see llms.txt. Markdown versions of all pages are available by appending .md to any URL (e.g. /docs/manual/basics.md).

NumPyView

struct NumPyView[mut: Bool, //, dtype: DType, rank: Int, origin: Origin[mut=mut]]

A borrowed view of a C-contiguous NumPy array's buffer and shape.

from_numpy_tensor returns this rather than a bare Span, so the extents needed to index the buffer travel with it. shape is a Coord, so it can be handed straight back to copy_to_numpy_tensor.

Parameters​

  • ​mut (Bool): The mutability of the borrow.
  • ​dtype (DType): The element dtype.
  • ​rank (Int): The number of dimensions.
  • ​origin (Origin[mut=mut]): The origin of the borrow.

Fields​

  • ​data (Span[Scalar[dtype], origin]): The array's elements, in C order.
  • ​shape (Coord[*#kgen.param_list.tabulate(rank, [idx: __mlir_type.index] Int)]): The extent of each axis. Subscript it with a compile-time index.

Implemented traits​

AnyType, Copyable, Deinitable, ImplicitlyCopyable, Movable

Methods​

__getitem__​

def __getitem__[*Ts: Intable](self, *idx: *Ts.values) -> ref[origin] Scalar[dtype]

Returns a reference to the element at idx.

Constraints:

The number of indices must equal rank.

Parameters:

  • ​*Ts (Intable): The type of each index argument.

Args:

  • ​*idx (*Ts.values): One index per axis.

Returns:

ref[origin] Scalar[dtype]: A reference to the element, mutable if the view is.