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
Allow reversed(memoryview), like memoryview #63278
Comments
Hello. The following seems a little weird: Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:03:43) [MSC v.1600 32 bit (Intel)] on win32
>>> m = memoryview(b'123')
>>> list(m[::-1])
[51, 50, 49]
>>> list(reversed(m))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: object of type 'memoryview' has no len()
>>> The attached patch allows |
So the dilemma with len() was: does it return the number of bytes, or the number of items? |
Aye, with the 3.3 changes, I think we should continue down the |
So, in the following case, len shouldn't return 4, but the number of items? Also, is it ok to assume that the number of items is >>> x = np.array([[1, 2, 3], [4, 5, 6], [4,5,6], [4,4,4]], np.int32)
>>> x.shape
(4, 3)
>>> x.shape[0] * x.shape[1]
12
>>> len(x)
4
>>> memoryview(x)
<memory at 0x0217C378>
>>> len(memoryview(x))
4
>>> memoryview(x).shape
(4, 3)
>>> |
No, you're right, it should probably return 4. |
Yes, len() should return the number of items. +1 for making reversed() NumPy does this: >>> x = numpy.array([1,2,3,4,5,6,7,8])
>>> list(reversed(x))
[8, 7, 6, 5, 4, 3, 2, 1]
>>>
>>> x = numpy.array([[1,2,3], [4,5,6]])
>>> list(reversed(x))
[array([4, 5, 6]), array([1, 2, 3])]
>>> |
Hmm, I meant: Number of items in the first dimension, thus 4 in |
For multidimensional arrays it doesn't seem to work (yet). >>> x = numpy.array([[1,2,3], [4,5,6]])
>>> list(reversed(x))
[array([4, 5, 6]), array([1, 2, 3])]
>>> x.data
<memory at 0x8032d06b8>
>>> list(reversed(x.data))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NotImplementedError: multi-dimensional sub-views are not implemented
>>> |
Stefan, what do you think about Claudiu's patch? Should a test be added to test_buffer as well? |
Claudiu's patch looks correct. |
I think the patch is good. We can add more tests when (if?) multi-dimensional In that case we should probably do the same as NumPy and return a list of I can't commit right now (the machine with my ssh-key won't have Internet |
New changeset 0dc604d58949 by Nick Coghlan in branch 'default': |
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: