In issue ( Can't read a F-contiguous memoryview in physical order · Issue #80026 · python/cpython · GitHub ), ( Memoryview for column-major (f_contiguous) arrays from bytes impossible to achieve · Issue #78959 · python/cpython · GitHub ), and issue ( Supporting `strides` in `memoryview.cast` · Issue #85398 · python/cpython · GitHub ), it is brought up that handling striding other that C-contiguous (like F-contiguous) is kind of tricky with memoryview
. There are a few ways one might go about improving on this:
- Allow users to override
strides
(say inmemoryview.cast(...)
) - Provide an
order
flag for operations (likememoryview.cast(...)
) - Add a
.T
attribute tomemoryview
to allow transposing (reversingmemoryview.strides
) - Add
.transpose(...)
tomemoryview
to allow specifying the new axis order
There is probably some value for any of these changes. Several array libraries implement things like these.
Personally would argue manipulating strides
directly while quite powerful is often more difficult for people to understand. Similarly .transpose(...)
provides a lot of flexibility and is a bit easier to understand, it often dovetails well with other operations a fully fledged array library might provide.
This leaves (2) adding an order
parameter to .cast(...)
and (3) adding .T
attribute. Both would be fine and are fairly easy to understand. Though if we had to choose, would lean towards .T
as it can already be combined easily with normal .cast(...)
operations and can be valuable on its own (moving from F-contiguous to C-contiguous for example). Also .T
should be fairly simple to implement (copy the memoryview
data as in .cast(...)
and reverse the strides
).
Would be curious to hear others thoughts on these and any other proposals one might have. Also curious about your preferences as well. Thanks for your feedback!