stopiteration

Python PEP479 Change StopIteration handling inside generators

落花浮王杯 提交于 2019-12-01 00:47:57
问题 Could someone help me understand what PEP479 is about? I was reading the doc and couldn't get my head around it. The abstract says: This PEP proposes a change to generators: when StopIteration is raised inside a generator, it is replaced it with RuntimeError. (More precisely, this happens when the exception is about to bubble out of the generator's stack frame.) So for example, does a loop like so still work? it = iter([1,2,3]) try: i = next(it) while True: i = next(it) except StopIteration:

What is the difference between raise StopIteration and a return statement in generators?

拜拜、爱过 提交于 2019-11-30 06:39:50
问题 I'm curious about the difference between using raise StopIteration and a return statement in generators. For example, is there any difference between these two functions? def my_generator0(n): for i in range(n): yield i if i >= 5: return def my_generator1(n): for i in range(n): yield i if i >= 5: raise StopIteration I'm guessing the more "pythonic" way to do it is the second way (please correct me if I'm wrong), but as far as I can see both ways raise a StopIteration exception. 回答1: There's

What is the difference between raise StopIteration and a return statement in generators?

北城以北 提交于 2019-11-30 04:14:41
I'm curious about the difference between using raise StopIteration and a return statement in generators. For example, is there any difference between these two functions? def my_generator0(n): for i in range(n): yield i if i >= 5: return def my_generator1(n): for i in range(n): yield i if i >= 5: raise StopIteration I'm guessing the more "pythonic" way to do it is the second way (please correct me if I'm wrong), but as far as I can see both ways raise a StopIteration exception. There's no need to explicitly raise StopIteration as that's what a bare return statement does for a generator

“RuntimeError: generator raised StopIteration” every time I try to run app

心已入冬 提交于 2019-11-30 03:21:28
I am trying to run this code: import web urls = ( '/', 'index' ) if __name__ == "__main__": app = web.application(urls, globals()) app.run() But it gives me this error everytime C:\Users\aidke\Desktop>python app.py Traceback (most recent call last): File "C:\Users\aidke\AppData\Local\Programs\Python\Python37-32\lib\site-packages\web\utils.py", line 526, in take yield next(seq) StopIteration The above exception was the direct cause of the following exception: Traceback (most recent call last): File "app.py", line 14, in <module> app = web.application(urls, globals()) File "C:\Users\aidke

“RuntimeError: generator raised StopIteration” every time I try to run app

蹲街弑〆低调 提交于 2019-11-28 23:43:54
问题 I am trying to run this code: import web urls = ( '/', 'index' ) if __name__ == "__main__": app = web.application(urls, globals()) app.run() But it gives me this error everytime C:\Users\aidke\Desktop>python app.py Traceback (most recent call last): File "C:\Users\aidke\AppData\Local\Programs\Python\Python37-32\lib\site-packages\web\utils.py", line 526, in take yield next(seq) StopIteration The above exception was the direct cause of the following exception: Traceback (most recent call last):

How yield catches StopIteration exception?

烂漫一生 提交于 2019-11-27 20:19:36
问题 Why in the example function terminates: def func(iterable): while True: val = next(iterable) yield val but if I take off yield statement function will raise StopIteration exception? EDIT: Sorry for misleading you guys. I know what generators are and how to use them. Of course when I said function terminates I didn't mean eager evaluation of function. I just implied that when I use function to produce generator: gen = func(iterable) in case of func it works and returns the same generator, but

Why does next raise a 'StopIteration', but 'for' do a normal return?

折月煮酒 提交于 2019-11-27 00:49:09
In this piece of code, why does using for result in no StopIteration or is the for loop trapping all exceptions and then silently exiting? In which case, why do we have the extraneous return ?? Or is the raise StopIteration caused by: 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) Assuming StopIteration is being triggered by: return None . When is GeneratorExit generated? def countdown(n): print("Counting down from %d" % n) try: while n > 0: yield n n = n