Memoryview Attributes

memoryview.ndim

Report the number of dimensions in the logical layout. A byte sequence normally has one dimension; casting can create a rectangular multidimensional layout or a scalar view with zero dimensions. In Python 3.13 len() on a zero-dimensional view raises TypeError. This read-only value equals the number of entries in shape and strides, not the element count.

Formal Syntax & Structure

python syntaxpython
view.ndim

Return Value

int

Dimension count.

Working Example

Example Snippetpython
flat = memoryview(b"ABCDEF")print(flat.ndim)print(flat.cast("B", [2, 3]).ndim)print(memoryview(b"A").cast("B", ()).ndim)
Python Reference