-
-
Notifications
You must be signed in to change notification settings - Fork 30.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
memoryview doesn't allow tuple-indexing #67820
Comments
It is a bit of pity. A least non-sliced indexing should be able to work: >>> import numpy as np
>>> a = np.arange(10)
>>> memoryview(a)[0]
0
>>> a = np.arange(10).reshape((2,5))
>>> a[0,1]
1
>>> memoryview(a)[0,1]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: memoryview: invalid slice key |
Here is a patch. |
Aren't there wider implications of python starting to support tuple indexing? If we make this work, aren't people going to expect [[1,2], [3,4]][0,1] to work? |
Or even [[1,2], [3,4]][*(0,1)] :-) But seriously, I don't know. memorview is a pretty specialized object, its semantics have more to do with Numpy (which has been a major inspiration and use case for the buffer protocol's design) than standard Python containers. I wouldn't expect a list of lists to support tuple indexing. |
Multi-dimensional slicing is explicitly mentioned in PEP-3118, so |
To be sure, the PEP contains some odd proposals like "Unpacking a long-double will return a decimal object", but the one in this issue seems reasonable. |
May be add support of multi-dimensional sub-views? >>> m = memoryview(bytearray(range(12)))
>>> m2 = m.cast('B', (3,4))
>>> m2[0]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NotImplementedError: multi-dimensional sub-views are not implemented Then we could use m2[0][1]. |
Sub-views are not as easily implemented. Besides, they create an intermediate object which is immediately thrown away. And tuple indexing is a very common idiom in the Numpy universe. |
sub-views and multi-dimensional slicing are not that bad: They're already implemented in _testbuffer (indeed with intermediate objects though). |
Well, we could certainly do both :) I don't think we should weigh this issue with separate features, though. |
Yes, to be clear I'm +1 on this specific feature -- and separate issues. :) |
It turns out that msg237933 was too simplistic: There are some http://docs.scipy.org/doc/numpy/reference/arrays.indexing.html In particular, x[::2, 1, ::2] is not necessarily equal to |
Do you want to review the patch? |
I left few nitpicks on Rietveld, but in general the patch LGTM. Oh, needed a documentation. |
Updated patch addressing Serhiy's comments. |
It would be good to add examples with multidimensional memoryviews. For example: >>> a = array.array('l', [-11111111, 22222222, -33333333, 44444444])
>>> m = memoryview(a).cast('B').cast('l', [2, 2])
>>> m[0, 0]
-11111111
>>> m[1, 0]
-33333333
>>> m[-1, -1]
44444444 |
New changeset 7d4eb5902f82 by Antoine Pitrou in branch 'default': |
I didn't add the 2d example as the double cast is quite ugly. Probably we should allow casting when the format stays the same. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: