Is there a common idiom to avoid pointless copying of fragments for such cases:
>>> a = bytearray(b'hello')
>>> b = bytearray(b'goodbye, cruel world.')
>>> a.extend(b[14:20])
>>> a
bytearray(b'hello world')
It seems to me that there is an unnecessary copy when creating a slice b[14:20]. Instead of creating a new fragment in memory to give extend, I want to say "use only this range of the current object."
Some methods will help you with the cut parameters, for example count:
>>> a = bytearray(1000000)
>>> a[0:900000].count(b'\x00')
900000
>>> a.count(b'\x00', 0, 900000)
900000
but many, for example extendin my first example, do not have this function.
I understand that for many applications, what I'm talking about would be micro-optimization, so before anyone asks - yes, I profiled my application, and this is something to worry about about my case.
"" , .