The Yield statement

Jorgen Grahn grahn+nntp at snipabacken.se
Mon Jul 7 13:43:16 EDT 2008


On Sun, 6 Jul 2008 00:29:26 -0700, Mark Tolonen <M8R-yfto6h at mailinator.com> wrote:
>
> "inhahe" <inhahe at gmail.com> wrote in message 
> news:z6_bk.24960$AJ6.1423 at bignews8.bellsouth.net...
...

>> note that this won't work;
>> def f():
>>  for x in xrange(10):
>>    for y in xrange(10):
>>      yield (x,y)
>>
>> yield just doesn't work right with multiple levels of loops. i had to 
>> discover that the hard way.
>
> Huh?  Works fine: [...]

Yes -- as is expected.  Yield would be much less useful if it only
work with strange restrictions on the structure of the function
containing it.

As far as I can tell, you can pick any function, stick zero or more
yield statements into it, and it will work as expected: hit a yield,
and you get another value for your sequence.

/Jorgen

-- 
  // Jorgen Grahn <grahn@        Ph'nglui mglw'nafh Cthulhu
\X/     snipabacken.se>          R'lyeh wgah'nagl fhtagn!



More information about the Python-list mailing list