Questions tagged «stopiteration»

1
为什么下一个为什么会引发“ StopIteration”,而“ for”却会正常返回?
在这段代码中,为什么使用for结果为noStopIteration 或for循环捕获所有异常然后静默退出?在这种情况下,为什么会有多余的return?还是由以下 raise StopIteration原因引起的return None? #!/usr/bin/python3.1 def countdown(n): print("counting down") while n >= 9: yield n n -= 1 return for x in countdown(10): print(x) c = countdown(10) next(c) next(c) next(c) 假设StopIteration由触发return None。什么时候GeneratorExit产生的? def countdown(n): print("Counting down from %d" % n) try: while n > 0: yield n n = …
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.