Created
March 8, 2017 19:30
-
-
Save juanarrivillaga/e6a68d70cbae3d2ca72f3d81f869d4f6 to your computer and use it in GitHub Desktop.
What does a Python for-loop do?
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
# If we were to implement a Python for-loop in Python, | |
for x in my_iterable: | |
do_stuff(x) | |
# it would be equivalent to the following: | |
iterator = iter(my_iterable) | |
while True: | |
try: | |
x = next(iterator) | |
except StopIteration: | |
break | |
do_stuff(x) | |
# note iter(something) returns something.__iter()__ | |
# and next(another_thing) returns another_thing.__next__() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment