Created
February 21, 2021 22:58
-
-
Save eddieantonio/6bcccd9f42332f2887663138b08f4ae4 to your computer and use it in GitHub Desktop.
Get the slice[start:stop:step] of any Python iterable
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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