Why don't generators execute until first yield?

Marco Mariani marco at sferacarta.com
Wed May 7 08:24:34 EDT 2008


Duncan Booth wrote:

> It does this:
> 
>>>> @greedy
> def getCommandsFromUser():
> 	while True:
> 		yield raw_input('Command?')
> 
> 		
>>>> for cmd in getCommandsFromUser():
> 	print "that was command", cmd
> 
> 	
> Command?hello
> Command?goodbye
> that was command hello
> Command?wtf
> that was command goodbye
> Command?


Not here..


In [7]: def getCommandsFromUser():
         while True:
                 yield raw_input('Command?')
    ...:
    ...:

In [10]: for cmd in getCommandsFromUser(): print "that was command", cmd
    ....:
Command?hi
that was command hi
Command?there
that was command there
Command?wuwuwuw
that was command wuwuwuw
Command?



More information about the Python-list mailing list