Last active
December 12, 2015 05:58
-
-
Save jbeluch/4726017 to your computer and use it in GitHub Desktop.
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 BetterIter(object): | |
def __init__(self, items): | |
self.items = items[:] | |
self._max_index = len(self.items) - 1 | |
self._index = 0 | |
def __getitem__(self, key): | |
try: | |
return self.items[key] | |
except IndexError: | |
return None | |
@property | |
def current_item(self): | |
return self[self._index] | |
def next(self): | |
self._index += 1 | |
if self._index > self._max_index: | |
self._index = 0 | |
return self.current_item | |
def prev(self): | |
self._index -= 1 | |
if self._index < 0: | |
self._index = self._max_index | |
return self.current_item |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment