标签云

微信群

扫码加入我们

WeChat QR Code

This question already has an answer here:What does the “yield” keyword do?42 answersI 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 sysimport timedef spinning_cursor():cursor='/-\|'i = 0while 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)

2019年05月22日04分30秒

you may find everything just here : stackoverflow.com/questions/231767/…

2019年05月22日04分30秒

Another example of how yield was working with a function here... I don't need an example now that I understand. Also, thanks pylover

2019年05月22日04分30秒

Waaaay too fast.

2019年05月22日04分30秒

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.

2019年05月22日04分30秒

Great stuff thank you.

2019年05月22日04分30秒