# Memoryview multi-dimensional slicing support

**URL:** <https://discuss.python.org/t/memoryview-multi-dimensional-slicing-support/52776>\
**Category:** Ideas\
**Created:** [May 7, 2024, 1:01am UTC](https://discuss.python.org/t/memoryview-multi-dimensional-slicing-support/52776 "2024-05-07T01:01:48Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![dg-pb](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/dg-pb/32/16248_2.png) [@dg-pb](https://discuss.python.org/u/dg-pb)\
**Post date:** [May 7, 2024, 1:01am UTC](https://discuss.python.org/t/memoryview-multi-dimensional-slicing-support/52776/1 "2024-05-07T01:01:48Z")

</div>

[PEP 3118 – Revising the buffer protocol | peps.python.org](https://peps.python.org/pep-3118/) states:

```python
__getitem__ (will support multi-dimensional slicing)
__setitem__ (will support multi-dimensional slicing)

```

However:

```python
mv = memoryview(b'\x00').cast('b', (1, 1))
mv[:,:1]
---------------------------------------------------------------------------
NotImplementedError: multi-dimensional slicing is not implemented

```

Has anyone else missed this?

Don’t think much would change if only slicing was to be implemented.

However, it might open a fairly big window if full featured indexing as per [https://data-apis.org/array-api](https://data-apis.org/array-api) was to be implemented.

---

<div class="post-metadata">

**Author:** ![vberlier](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/vberlier/32/19354_2.png) [@vberlier](https://discuss.python.org/u/vberlier)\
**Post date:** [May 15, 2024, 11:50pm UTC](https://discuss.python.org/t/memoryview-multi-dimensional-slicing-support/52776/2 "2024-05-15T23:50:20Z")

</div>

As you pointed out, the buffer protocol is the foundation of a pretty extensive API surface. Python wouldn’t be able to maintain its own complete implementation. So my understanding has always been that Python’s own `memoryview` builtin was an intentionally limited utility that only implements the minimum required to perform low-level operations on buffers normally managed by external libraries. By low-level operations I mean things like preventing a buffer from being garbage collected and transferring it from one library to another. For high-level features, like multi-dimensional slicing, you’re meant to use the high-level abstractions provided by external libraries, like numpy arrays.

But it’s also true that there’s no technical limitation here, the `memoryview` builtin could reasonably support some of the more simple APIs as long as it doesn’t become too much of a burden for the core maintainers.

---

<div class="post-metadata">

**Author:** ![dg-pb](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/dg-pb/32/16248_2.png) [@dg-pb](https://discuss.python.org/u/dg-pb)\
**Post date:** [May 16, 2024, 12:22am UTC](https://discuss.python.org/t/memoryview-multi-dimensional-slicing-support/52776/3 "2024-05-16T00:22:34Z")

</div>

> [@vberlier](#):
>
> But it’s also true that there’s no technical limitation here, the `memoryview` builtin could reasonably support some of the more simple APIs as long as it doesn’t become too much of a burden for the core maintainers.

I am a bit in a pickle. I intend to write some basic array infrastructure, which is not to be used in scientific computing, but serve as utility for dependency free parts of infrastructure - e.g. standard library extensions, where anything like numpy is not and shouldn’t be a dependency.

The last thing I want to do is to write my own implementation in C. And `memoryview` almost fits the purpose - the only thing it lacks is multi-dimensional indexing.

If there were both ` __getitem__ ` and ` __setitem__ ` implemented as per Array API, it would be possible to implement a pretty good tensor object without any C work. With these 2 implemented, many other features can be implemented efficiently.

---

<div class="post-metadata">

**Author:** ![vberlier](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/vberlier/32/19354_2.png) [@vberlier](https://discuss.python.org/u/vberlier)\
**Post date:** [May 17, 2024, 1:20pm UTC](https://discuss.python.org/t/memoryview-multi-dimensional-slicing-support/52776/4 "2024-05-17T13:20:56Z")

</div>

Not sure how you could make an actually useful tensor object based on `memoryview` without also implementing a bunch of item-wise operations in C though. Even if the full ` __getitem__ ` and ` __setitem__ ` were implemented natively, you’d still have to iterate in python, repeatedly boxing values to perform any operation. At this point there wouldn’t be much benefit over subclassing or wrapping `memoryview` to implement multi-dimensional slicing in python yourself.

---

<div class="post-metadata">

**Author:** ![dg-pb](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/dg-pb/32/16248_2.png) [@dg-pb](https://discuss.python.org/u/dg-pb)\
**Post date:** [May 17, 2024, 2:35pm UTC](https://discuss.python.org/t/memoryview-multi-dimensional-slicing-support/52776/5 "2024-05-17T14:35:48Z")

</div>

Thanks, I think I need to think it over one more time.
