Python Generators

Peter Otten __peter__ at web.de
Sat Mar 15 18:09:57 EDT 2008


mpc wrote:

> I am trying to write a while loop that will iterate over generators to
> capture all the headers of FFCache directories. However, the
> generators embedded within the argument of another generator do not
> seem to re-initiate. the example below loops through and initiates the
> generator embedded in the argument only once. Can anyone explain while
> the generator will not re-initiate, and suggest a simple fix?

A generator or generator expression  does indeed only run once. 

>>> gen = (i for i in range(5) if i%2)
>>> for i in range(3):
...     print "---", i, "---"
...     for k in gen: print k,
...     print
...
--- 0 ---
1 3
--- 1 ---

--- 2 ---

The fix is to use a list or list comprehension, or make a new generator
every time you need one:

>>> for i in range(3):
...     print "---", i, "---"
...     gen = (i for i in range(5) if i%2)
...     for k in gen: print k,
...     print
...
--- 0 ---
1 3
--- 1 ---
1 3
--- 2 ---
1 3

At first glance I would guess that in your case all_caches is the culprit
that has to be moved into the 'while True: ...' loop. 

> while True:
>     n += 1
>     time.sleep(0.5)

      all_caches = (path for path,dirlist,filelist in os.walk("/Users/") if
          '_CACHE_MAP_' in filelist)

>     for path in all_caches:
>         headers  =  generate_headers(path)
>         for h in headers:
>             print h,n

(Untested, because you didn't bother to provide a self-contained example)

Peter



More information about the Python-list mailing list