Created
March 18, 2014 04:03
-
-
Save PuercoPop/9613366 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
for item in collection: | |
do_stuff(item) | |
#becomes: | |
iterator = iter(collection) | |
while True: | |
try: | |
item = iterator.next() | |
# The code from the for indent block goes here. (No lambdas :/) | |
do_stuff(item) | |
except StopIteration: | |
# The code from the else clause goes here. | |
break | |
# Don't trust me try this: | |
collection = iter([1, 2, 3, 4, 5]) # Python lists are iterable, but not | |
# iterators | |
while True: | |
try: | |
item = collection.next() | |
print "{0} bottles of beer on the wall".format(item) | |
except StopIteration: | |
break |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment