Do Python generator objects become "unusable" after passing?

I was working on a Flask project, getting some data from an API wrapper. The wrapper returned the generator object, so I print values ​​( for obj in gen_object: print obj ) before passing it to Flask render_template() .

When requesting a page while print objects, the page is empty. But when you delete the for loop, the page displays the contents of the generator object.

 @app.route('/') def front_page(): top_stories = r.get_front_page(limit=10) # this for loop prevents the template from rendering the stories for s in top_stories: print s return render_template('template.html', stories=top_stories) 
+7
source share
1 answer

Yes, generators must be consumed once. Each time we iterate over the generator, we ask it to give us a different value, and if no more values ​​are added to eliminate the StopIteration exception, which would stop the iteration. There is no way for the generator to know that we want to repeat it without cloning it.

While the records can fit comfortably in memory, I would use a list instead:

 top_stories = list(top_stories) 

This way you can iterate over top_stories multiple times.

There, a function called itertools.tee copies an iterator, which can also help you, but sometimes more slowly than just using a list. Help: http://docs.python.org/library/itertools.html?highlight=itertools.tee#itertools.tee

+9
source

All Articles