bytearrays and memoryview #14071
Unanswered
kjm1102
asked this question in
Using MicroPython
Replies: 1 comment
-
@kjm1102 This scenario (slice assignment) is basically the one case where memoryview doesn't help. Memoryview allows you to make read/write slices of some original buffer, without copying the contents of the buffer. Consider instead: def foo(buf):
print(buf.hex())
b = bytes(range(10))
foo(b[1:4])
m = memoryview(b)
foo(m[1:4]) Both print the same thing, but the first call to Maybe a better example: def foo(buf):
buf[2] = 0
print(buf.hex())
b = bytearray(range(10))
foo(b[1:4])
print(b.hex())
m = memoryview(b)
foo(m[1:4])
print(b.hex()) It prints
|
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I'm working my way through stuff the rest of the forum members seem to have got sorted back in 2016 so please excuse my late to the party but memoryview has me confused.
I can change a byte array without memory view
or with it
since both alter the original bytearray I don't understand the advantage of the memoryview method? Does the memoryview technique offer some advantage if the bytearray is large (20k)?
Beta Was this translation helpful? Give feedback.
All reactions