Skip to content

Instantly share code, notes, and snippets.

@juanarrivillaga
Created March 8, 2017 19:30
Show Gist options
  • Save juanarrivillaga/e6a68d70cbae3d2ca72f3d81f869d4f6 to your computer and use it in GitHub Desktop.
Save juanarrivillaga/e6a68d70cbae3d2ca72f3d81f869d4f6 to your computer and use it in GitHub Desktop.
What does a Python for-loop do?
# 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