Why don't generators execute until first yield?

Martin Sand Christensen msc at es.aau.dk
Wed May 7 07:24:22 EDT 2008


>>>>> "Duncan" == Duncan Booth <duncan.booth at invalid.invalid> writes:
[...]
Duncan> Now try:
Duncan> 
Duncan>    for command in getCommandsFromUser():
Duncan>        print "the result of that command was", execute(command)
Duncan> 
Duncan> where getCommandsFromUser is a greedy generator that reads from stdin, 
Duncan> and see why generators don't work that way.

I don't see a problem unless the generator isn't defined where it's
going to be used. In other similar input bound use cases, such as the
generator iterating over a query result set in my original post, I see
even less of a problem. Maybe I'm simply daft and you need to spell it
out for me. :-)

Martin



More information about the Python-list mailing list