"flushing"/demanding generator contents - implications for injection of control

metaperl metaperl at gmail.com
Mon Feb 5 14:47:43 EST 2007


For this program:

def reverse(data):
    for index in range(len(data)-1, -1, -1):
        yield data[index]

r = reverse("golf")

for char in r:
    print char


I'm wondering if the line:

r = reverse("golf")

"demands" the contents of the function reverse() all at once and if I
must write

for char in reverse("golf"):
    print char

if I want the results streamed instead of generated complely.

** CONTEXT **

The simple example above is not what I am really doing. My real
program parses very large
data files using pyparsing. Pyparsing can generate incremental/yielded
results with no problem:

http://pyparsing.wikispaces.com/message/view/home/248539#248852

but because I believe in injection of control (pushing data around as
opposed to having
the target pull it), I get the parse and then inject it into the
generator:

            parse = parsing.parse(fp.read())
            txt = textgen.generate(self.storage.output, patent_key,
parse, f.basename(), debug=False)




More information about the Python-list mailing list