No, there is only one thread.
Each iteration of the for loop executes your countFrom function until it gives something or returns. After the exit, the body of the for loop starts again, and then, when a new iteration begins, the countFrom function selects exactly where it was stopped, and starts again until it gives (or returns).
This modified version of your example will help make it clearer what the path is.
def countfrom(n): while n <= 12: print "before yield, n = ", n yield n n += 1 print "after yield, n = ", n for i in countfrom(10): print "enter for loop, i = ", i print i print "end of for loop iteration, i = ", i
Exit
before yield, n = 10 enter for loop, i = 10 10 end of for loop iteration, i = 10 after yield, n = 11 before yield, n = 11 enter for loop, i = 11 11 end of for loop iteration, i = 11 after yield, n = 12 before yield, n = 12 enter for loop, i = 12 12 end of for loop iteration, i = 12 after yield, n = 13
David heffernan
source share