Return multiple values over time

后端 未结 2 1226
深忆病人
深忆病人 2020-12-21 10:13

Okay, so is there a way to return a value from a function - the way return does - but not stop the function - the way return does?

2条回答
  •  春和景丽
    2020-12-21 10:52

    I think you are looking for yield. Example:

    import time
    
    def myFunction(limit):
        for i in range(0,limit):
            time.sleep(2)
            yield i*i
    
    for x in myFunction(100):
        print( x )
    

提交回复
热议问题