Created
August 28, 2014 20:22
-
-
Save capttwinky/7bcb49f5ba8a5a85a4af to your computer and use it in GitHub Desktop.
None safe iterators
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
import collections | |
def strict_iterator(iterable): | |
if not isinstance(iterable, collections.Iterable): | |
raise StopIteration | |
else: | |
for i in iterable: | |
yield i | |
def safe_iterator(iterable): | |
try: | |
for i in iterable: | |
yield i | |
except TypeError: | |
raise StopIteration |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment