[Python-Dev] PEP 467: Minor API improvements to bytes, bytearray, and memoryview

Serhiy Storchaka storchaka at gmail.com
Wed Jun 8 05:11:40 EDT 2016

On 08.06.16 02:03, Nick Coghlan wrote:
> That said, it occurs to me that there's a reasonably strong
> composability argument in favour of a view-based approach: a view will
> work with operator.itemgetter() and other sequence consuming APIs,
> while special methods won't. The "like-memoryview-but-not" view type
> could also take any bytes-like object as input, similar to memoryview
> itself.

Something like:

class chunks:
     def __init__(self, seq, size):
         self._seq = seq
         self._size = size

     def __len__(self):
         return len(self._seq) // self._size

     def __getitem__(self, i):
         chunk = self._seq[i: i + self._size]
         if len(chunk) != self._size:
             raise IndexError
         return chunk

(but needs more checks and slices support).

It would be useful for general sequences too.

More information about the Python-Dev mailing list