Fwd: Python 3.3 can't sort memoryviews as they're unorderable
-------- Original Message --------
Subject: Python 3.3 can't sort memoryviews as they're unorderable
Date: Sun, 21 Oct 2012 12:24:32 +0100
From: Mark Lawrence
memoryview(bytearray(range(5))) == memoryview(bytearray(range(5))) True memoryview(bytearray(range(5))) != memoryview(bytearray(range(5))) False memoryview(bytearray(range(5))) < memoryview(bytearray(range(5))) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: unorderable types: memoryview() < memoryview()
Okay then, let's subclass memoryview to provide the functionality.
class Test(memoryview): ... pass ... Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: type 'memoryview' is not an acceptable base type
Oh dear. http://docs.python.org/py3k/library/stdtypes.html#typememoryview only gives examples of equality comparisons and there was nothing that I could see in PEP3118 to explain the rationale behind the lack of other comparisons. What have I missed? Nobody on the main Python ml could answer this so can someone please explain the background to how memoryviews work in this instance as I'm confused, thanks. Mark Lawrence.
(Oops, originally replied only to Mark) Is a 3x3 array greater or less than a 2x4 array or another 3x3 array? The contents of a 1D memory view may be sortable, but the "logical structure" part isn't, and neither is any multi-dimensional view. I'm surprised by the lack of inheritance support though - is that a regression from 3.2? If yes, that's definitely a bug to be fixed in a 3.3 maintenance release, otherwise it's probably a feature request for 3.4. Cheers, Nick. -- Sent from my phone, thus the relative brevity :)
On 24/10/2012 13:19, Nick Coghlan wrote:
(Oops, originally replied only to Mark)
Is a 3x3 array greater or less than a 2x4 array or another 3x3 array?
The contents of a 1D memory view may be sortable, but the "logical structure" part isn't, and neither is any multi-dimensional view.
I'm surprised by the lack of inheritance support though - is that a regression from 3.2? If yes, that's definitely a bug to be fixed in a 3.3 maintenance release, otherwise it's probably a feature request for 3.4.
Cheers, Nick.
-- Sent from my phone, thus the relative brevity :)
The lack of inheritance support is the same in Python 2.7.3 so I doubt that there's any change there. Quoting Nick Coghlan from http://bugs.python.org/issue15622#msg167957 "PEP 3118 contains way too many errors (as has been found out the hard way) to be considered a normative document.". I can't say that this gives me a great deal of confidence. It strikes me that a lot of code has been written, tested and released without having anything like a requirement. For example when is any given memoryview equal to or not equal to any other memoryview? -- Cheers. Mark Lawrence.
Mark Lawrence
I can't say that this gives me a great deal of confidence. It strikes me that a lot of code has been written, tested and released without having anything like a requirement. For example when is any given memoryview equal to or not equal to any other memoryview?
A lot of documentation has been written and released as well. Use it. In case you don't know: If a PEP and the current documentation diverge, the documentation takes precedence. Stefan Krah
On 25/10/2012 15:06, Stefan Krah wrote:
Mark Lawrence
wrote: I can't say that this gives me a great deal of confidence. It strikes me that a lot of code has been written, tested and released without having anything like a requirement. For example when is any given memoryview equal to or not equal to any other memoryview?
A lot of documentation has been written and released as well. Use it. In case you don't know: If a PEP and the current documentation diverge, the documentation takes precedence.
Stefan Krah
<Aussies please skip> Thanks for completely snipping the context that I gave, it gives me the sense that my bodyline tactics have you on the back foot. The documentation states "__eq__(exporter) A memoryview and a PEP 3118 exporter are equal if their shapes are equivalent and if all corresponding values are equal when the operands’ respective format codes are interpreted using struct syntax.". Where is the requirement that states this is all that the implementation provides? In the savaged PEP 3118? Why can't I have a situation such that two memoryviews that I've created that meet the criteria above shouldn't be tested using the other comparison operators? I'm guessing that I've missed something that's blatantly obvious to everybody except myself. I can't find a rationale anywhere as to why I can't subclass memoryviews for my code, so I can't work around what I perceive as a glaring deficiency. Is it a case whereby even consenting adults aren't allowed? This strikes me as so different to the Python that I've been using for the last 10 years that it stood out, at least to me, like a sore thumb. Perhaps I need yet another visit to the opticians? -- Cheers. Mark Lawrence.
On 26/10/12 02:57, Mark Lawrence complained that he can't subclass memoryviews:
I'm guessing that I've missed something that's blatantly obvious to everybody except myself. I can't find a rationale anywhere as to why I can't subclass memoryviews for my code, so I can't work around what I perceive as a glaring deficiency. Is it a case whereby even consenting adults aren't allowed?
This strikes me as so different to the Python that I've been using for the last 10 years that it stood out, at least to me, like a sore thumb. Perhaps I need yet another visit to the opticians?
Possibly. There are many things that you can't subclass in Python. NoneType NotImplementedType Ellipsis functions slices frames tracebacks and probably others. As annoying as it is when you run into something like this, it's hardly without precedent. Hell, for about half of Python's existence, you couldn't even subtype basic types like int, str and list! Subclassing is not the only way to add functionality to a class. While it is much less convenient with new-style classes than classic classes, why don't you try delegation? Actually, since the problem you are trying to solve is to sort a list of memoryviews, you don't even need delegation. You just need a dirt-simple key function. py> mv = memoryview py> x = list(map(mv, (b'xyz', b'abc', b'pqr'))) py> x.sort(key=lambda x: x.obj) py> [a.obj for a in x] [b'abc', b'pqr', b'xyz'] What was the problem again? :) -- Steven
participants (4)
-
Mark Lawrence
-
Nick Coghlan
-
Stefan Krah
-
Steven D'Aprano