This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Supporting `strides` in `memoryview.cast`
Type: enhancement Stage:
Components: Library (Lib) Versions: Python 3.11, Python 3.10, Python 3.9
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: jakirkham, veky
Priority: normal Keywords:

Created on 2020-07-07 02:01 by jakirkham, last changed 2022-04-11 14:59 by admin.

Messages (3)
msg373202 - (view) Author: (jakirkham) Date: 2020-07-07 02:01
Currently one can reshape a `memoryview` using `.cast(...)` like so...

```
In [1]: m = memoryview(b"abcdef")

In [2]: m2 = m.cast("B", (2, 3))
```

However it is not currently possible to specify the `strides` when reshaping the `memoryview`. This would be useful if the `memoryview` should be F-order or otherwise strided. To that end, syntax like this would be useful...

```
In [1]: m = memoryview(b"abcdef")

In [2]: m2 = m.cast("B", (2, 3), (1, 2))
```
msg402944 - (view) Author: Vedran Čačić (veky) * Date: 2021-09-30 10:15
I surely don't understand what the third argument means. What's (1, 2) there, and what stride does it produce?
msg410515 - (view) Author: (jakirkham) Date: 2022-01-13 19:54
The 2nd argument is the `strides`. IOW it is just specifying how to traverse the buffer in memory to visit each of the dimensions.

For the first example where `strides` is not specified, Python makes them C-ordered. IOW `m2.strides` would be `(3, 1)`. Effectively this is represented like this:

```
[[ b"a", b"c", b"e"],
 [ b"b", b"d', b"f"]]
```

For the second case where strides are overridden (so `m2.strides` would be `(1, 2)`), we get something like this:

```
[[b"a", b"b", b"c"],
 [b"d", b"e", b"f"]]
```

In either case the `1` here has specified which dimension is fastest to traverse along. IOW that content is adjacent in memory.

Should add the reason it is `1` is that for `uint8_t` (or format "B"), this is that type's size. If we had a different format, this would be the size of that format.

HTH
History
Date User Action Args
2022-04-11 14:59:33adminsetgithub: 85398
2022-01-13 19:54:52jakirkhamsetmessages: + msg410515
2021-09-30 10:15:20vekysetnosy: + veky
messages: + msg402944
2021-09-30 00:30:19jakirkhamsettype: enhancement
components: + Library (Lib)
2021-09-27 19:07:40jakirkhamsetversions: + Python 3.11
2020-07-07 02:01:27jakirkhamcreate