标签云

微信群

扫码加入我们

WeChat QR Code

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)

2018年08月16日26分46秒

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

2018年08月15日26分46秒

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

2018年08月16日26分46秒

Waaaay too fast.

2018年08月16日26分46秒

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.

2018年08月16日26分46秒

Great stuff thank you.

2018年08月16日26分47秒