Avoiding unnecessary copying of fragments in Python

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 million zero bytes
>>> a[0:900000].count(b'\x00')   # expensive temporary slice
900000
>>> a.count(b'\x00', 0, 900000)  # helpful start and end parameters
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.

"" , .

+5
2

buffer , :

>>> a.extend(buffer(b, 14, 6))
>>> a
bytearray(b'hello world')

, buffer . , . , .

, Python 3 (, , Python 2.7) memoryview:

>>> a.extend(memoryview(b)[14:20])
+5

itertools islice. islice count, , . ,

>>> from itertools import islice
>>> a = bytearray(1000000)
>>> sum(1 for x in islice(a,0,900000) if x==0)
900000
>>> len(filter(b'\x00'.__eq__,islice(a,0,900000)))
900000

>>> a=bytearray(b"hello")
>>> b = bytearray(b'goodbye, cruel world.')
>>> a.extend(islice(b,14,20))
>>> a
bytearray(b'hello world')
+2

All Articles