stopiteration

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

"RuntimeError: generator raised StopIteration" every time I try to run app Question: 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:UsersaidkeDesktop>python app.py Traceback (most recent call last): File “C:UsersaidkeAppDataLocalProgramsPythonPython37-32libsite-packageswebutils.py”, line 526, in …

Total answers: 7

How yield catches StopIteration exception?

How yield catches StopIteration exception? Question: 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 …

Total answers: 5

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

Why does next raise a 'StopIteration', but 'for' do a normal return? Question: 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: …

Total answers: 1

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

What is the difference between raise StopIteration and a return statement in generators? Question: 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 …

Total answers: 3

How can I get a Python generator to return None rather than StopIteration?

How can I get a Python generator to return None rather than StopIteration? Question: I am using generators to perform searches in lists like this simple example: >>> a = [1,2,3,4] >>> (i for i, v in enumerate(a) if v == 4).next() 3 (Just to frame the example a bit, I am using very much …

Total answers: 2