Question

In Python v2.7, I have come across a for statement over a for generator where in the body of the loop the item which is the source of the for generator is side effected:

for ssid in (ssid for ssid in overlaps if ssid != subseq_id):
  overlaps = subseq_id_to_overlap_ssids_dict[ssid]

My question is whether the side effect to overlaps is supposed to affect the for generator? I hope not, but if so, what exactly does that mean? If I've processed the first matching element and now replaced overlaps, would the for index into the next location of the new value for overlaps?

Was it helpful?

Solution

Rebinding overlaps inside the loop won't have any effect on the loop (though mutating it obviously would). Rebinding subseq_iq in side the loop could affect the generator.

A for loop evaluates the expression to get an iterator once at the beginning of the loop: in this case the expression is simply the name overlaps. Once it has evaluated that expression it creates an iterator from the iterable and uses that iterator to work through the elements. The iterator depends on the object returned by the expression, it doesn't depend on the name used to identify that object (if indeed there is any name).

If this particular case the code is exactly equivalent to:

for ssid in overlaps:
  if ssid != subseq_id:
     overlaps = subseq_id_to_overlap_ssids_dict[ssid]

This is a clearer way to write it, but again rebinding overlaps doesn't change the sequence being used by for.

OTHER TIPS

A for loop operates by iterating its argument i.e. calling iter on its argument to produce an iterator, then calling next on the iterator until StopIteration is raised.

Your generator comprehension argument evaluates the name-expression overlaps before the for loop is entered, so rebinding the name overlaps will have no effect; mutating the object that overlaps refers to will have some effect, depending on how iterators over type(overlaps) operate. For example, iterators on list increment an index into the list.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top