Skip to content

Instantly share code, notes, and snippets.

@elifiner
Last active August 29, 2015 14:23
Show Gist options
  • Save elifiner/b4857b0b518be9684fb2 to your computer and use it in GitHub Desktop.
Save elifiner/b4857b0b518be9684fb2 to your computer and use it in GitHub Desktop.
class Restartable(object):
def __init__(self, iterable):
self._iterable = iterable
self._cache = None
def __iter__(self):
if not self._cache:
if iter(self._iterable) is iter(self._iterable):
self._cache = list(self._iterable)
else:
self._cache = self._iterable
return iter(self._cache)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment