Last active
August 29, 2015 14:23
-
-
Save elifiner/b4857b0b518be9684fb2 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 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