cdwilson wrote: ↑Sat Sep 21, 2019 2:04 am
1. The reason why this was allowed in MicroPython but not in CPython? (I'm actually not sure why this is disallowed in CPython)
The simple answer would be that it's extra code to check this case. It also might just be an oversight.
cdwilson wrote: ↑Sat Sep 21, 2019 2:04 am
2. Are there any "gotchas" or things to be aware of when subclassing memoryview in MicroPython?
Like Peter said, support for subclassing built-ins is a bit unpredictable. One of the main contributors to this is that the implementations of the built-in classes assume they _aren't_ subclassed, so they don't do things like use the MRO to look up methods, or check types using the class hierarchy etc. (This mostly saves on code size).
One example for memoryview that might be surprising:
Code: Select all
class foo(memoryview):
def get_section(self):
return self[4:6]
b = b'abcdefghi'
f = foo(b)
f.get_section() --> this returns a memoryview, not a foo
That might actually be what you want, in that example