Slicing Buffers
Another important aspect of working with buffers is the ability to divide them into slices. A slice is a section of a buffer between a starting index and an ending index. Slicing a buffer allows you to manipulate a specific chunk.
You create slices by using slice([start], [end]), which returns a Buffer object that points to the start index of the original buffer and has a length of end – start. Keep in mind that a slice is different from a copy. If you edit a copy, the original does not change. However, if you edit a slice, the original does change.
The code in Listing 5.4 illustrates using slices. The important thing to note is that when the slice is altered in lines 5 and 6, it also alters the original buffer, as shown in
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access