Question

I need to have an original object (bytes) and this object minus last 20 bytes in python 3. I can do like this:

b = a[0:-20]

but it makes a copy of my object. Can I save memory linking to a part of an object?

Was it helpful?

Solution

Yeah, use a memoryview:

b = memoryview(a)[:-20]

As the documentation notes:

memoryview objects allow Python code to access the internal data of an object that supports the buffer protocol without copying.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top