Skip to content

Instantly share code, notes, and snippets.

@eddieantonio
Created February 21, 2021 22:58
Show Gist options
  • Save eddieantonio/6bcccd9f42332f2887663138b08f4ae4 to your computer and use it in GitHub Desktop.
Save eddieantonio/6bcccd9f42332f2887663138b08f4ae4 to your computer and use it in GitHub Desktop.
Get the slice[start:stop:step] of any Python iterable
class Sliceable:
def __init__(self, iterable):
self.it = iterable
def __getitem__(self, index):
if isinstance(index, int):
return self.it[int]
start = 0 if index.start is None else index.start
stop = inf if index.stop is None else index.stop
for idx, item in enumerate(self):
if idx < start:
continue
if idx >= stop:
break
yield item
def __iter__(self):
return iter(self.it)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment