jjzjj

GeneratorExit

全部标签

python - while 循环中的 GeneratorExit

我不清楚在while循环中捕获GeneratorExit的行为,这是我的代码:#pythonPython2.6.6(r266:84292,Sep42013,07:46:00)[GCC4.4.720120313(RedHat4.4.7-3)]onlinux2Type"help","copyright","credits"or"license"formoreinformation.>>>deffunc():...whileTrue:...try:...yield9...exceptGeneratorExit:...print"Needtodosomecleanup."...>>>g=fun

python - Python 生成器中的 GeneratorExit

我写了一个关于Python生成器的测试程序。但是我得到了一个意想不到的错误。我不知道如何解释。让我向您展示代码:defcountdown(n):logging.debug("Countingdown")whilen>0:try:yieldnexceptGeneratorExit:logging.error("GeneratorExit")n-=1if__name__=='__main__':c=countdown(10)logging.debug("value:%d",c.next())我认为它应该可以毫无问题地运行。但是输出是:#./test.py[2015/06/1604:10:4