Skip to content

Instantly share code, notes, and snippets.

@jbeluch
Last active December 12, 2015 05:58
Show Gist options
  • Save jbeluch/4726017 to your computer and use it in GitHub Desktop.
Save jbeluch/4726017 to your computer and use it in GitHub Desktop.
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