Why don't generators execute until first yield?

Duncan Booth duncan.booth at invalid.invalid
Wed May 7 08:31:19 EDT 2008


Marco Mariani <marco at sferacarta.com> wrote:

> 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?
> 

Perhaps if you'd copied all of my code (including the decorator that was 
the whole point of it)...




More information about the Python-list mailing list