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.
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.