Why use a generator object in this particular case?

I looked at some code that I downloaded from the Internet. This is for a basic web browser. I came across the following for loop:

 for link in (links.pop(0) for _ in xrange(len(links))): ... 

Now I feel that the following code will also work:

 for link in links: .... links=[] 

Research, I found out that the first instance clears links and also generates a generator object (genexpr) . links never used in a for loop, so its decreasing length has nothing to do with the code.

Is there any specific reason for using xrange and popping up elements every time? That is, is there any advantage in using the generator object over the calling elements of the standard list? In addition, in what cases the generator will be useful; why?

+4
source share
2 answers

It is not possible to see any justification for the code you provided.

The only thing I can think of is that the objects in links may be large or otherwise associated with limited resources, and therefore it may be important to free them as soon as possible (instead of waiting until the end of the loop to free them). But (a) if so, it would be better to process each link as it is created (possibly using a generator to organize the code), instead of creating the entire list of links before proceeding with its processing; and (b) even if you had no choice but to create the entire list before processing it, it would be cheaper to clear each entry in the list than to pop up the list:

 for i, link in enumerate(links): links[i] = None ... 

(The output of the first element from a list with n elements takes O (n), although in practice it will be pretty fast, because it is implemented using memmove .)

Even if you absolutely insisted that the list reappear as it repeats over it, it would be better to write a loop like this:

 while links: link = links.pop(0) ... 
+6
source

The goal of generators is to avoid creating large collections of intermediate objects that will not be used for external use.

If all the code builds a set of links on the page, the second code snippet is great. But, you might need a set of website root names (e.g. google.com, not google.com/q=some_search_term ....). If so, you should take a list of links and then view the complete list by deleting only the first part.

This is for this second clipping part, where you will get more using the generator. Instead of being useless to create a list of links that take up memory and assembly time, you can go through each link one by one, getting the site name without a large list of intermediaries for all links.

0
source

All Articles