What are the advantages of “profitability” and return to it (position)?

In the examples below, resp.results is an iterator.

Version1:

items = [] for result in resp.results: item = process(result) items.append(item) return iter(items) 

Version 2:

 for result in resp.results: yield process(result) 

Does it return (elements) in Version 1 better / worse in terms of performance / saving memory than just returning elements?

In the Python Cookbook, Alex says that explicit iter () is “more flexible, but less commonly used,” but what are the pros and cons of returning iter (items) versus output, as in version 2?

Also, what are the best ways to remove an iterator and / or exit? - you can not do len (results) to check the size of the list?

+6
source share
5 answers

It's easy to include an iterator or generator in the list if you need it:

 results = [item for item in iterator] 

Or, as kindly pointed out in the comments, an even simpler method:

 results = list(iterator) 
+4
source

The first leads to the fact that all the results are calculated and stored, and the second is lazy, while the results are calculated only upon request. That is, each will store and create a list of N elements, while the other will store and create 0 elements until you start repeating them.

It is better to think about this using ifilter (from itertools ), in which you do the same as the output, re generates an iterator instead of a generator:

  ifilter(process, resp.results) 

I found that iterators tend to run generators faster in the 2.x series, but I cannot verify the cost savings in the 3.x series.

+4
source

When you are processing a very large list, then the yield item better, since it does not consume much memory.

See the excellent article in the generator http://www.dabeaz.com/generators/Generators.pdf

+3
source

You can create infinite iterators, but not infinite lists:

 def fibGen(): f0, f1 = 0, 1 while True: yield f0 f0, f1 = f1, f0+f1 
+2
source

The pro and con of the first fragment is that all the results are calculated in advance. This is useful if the time between extracting each item is critical, but will not be fulfilled if the iterability is infinite or if space is a problem.

+1
source

Source: https://habr.com/ru/post/889729/


All Articles