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

Jussi Salmela tiedon_jano at hotmail.com
Mon Feb 5 15:08:41 EST 2007


metaperl kirjoitti:
> 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)
> 
I don't know, I'm guessing:

	... r = reverse("golf")
	... type(r)
	<type 'generator'>
	... print r.next()
	f

So r is not the string 'flog', it is the generator producing it

HTH,
Jussi



More information about the Python-list mailing list