This question already has an answer here:
I was looking for code to spin a cursor in the terminal and found this. I was wondering what was happening in the code. In particular
for c in spinning_cursor(): I've never seen this syntax. Is it because I am returning one element from a generator at a time with
yield, and this is assigned to c? Any other examples of this for x in y() use?
import sys import time def spinning_cursor(): cursor='/-\|' i = 0 while 1: yield cursor[i] i = (i + 1) % len(cursor) for c in spinning_cursor(): sys.stdout.write(c) sys.stdout.flush() time.sleep(0.1) sys.stdout.write('\b')
Another example? for i in range(10): print(i)
you may find everything just here : stackoverflow.com/questions/231767/…
Another example of how yield was working with a function here... I don't need an example now that I understand. Also, thanks pylover
Waaaay too fast.
Thanks Martijn. The idea is to call a function and while it is processing, display this spinning cursor. When it is finished processing it is stopped. So I will work on that. Thanks for the great answer.
Great stuff thank you.