[Tutor] I am having difficulty grasping 'generators'

Hilton Fernandes hgfernan at gmail.com
Wed May 28 17:35:32 CEST 2014


Dear all,

i'd like to thank every answer in this list. Alan Gauld is a fine writer of
excellent introductory material on Pyton, and so are a few other members of
this list.

So, it is always enlightening to read what you all write. Keep up the good
work.

All the best,
hilton



On Wed, May 28, 2014 at 8:57 AM, Alan Gauld <alan.gauld at btinternet.com>wrote:

> On 28/05/14 11:52, Degreat Yartey wrote:
>
>> This means that  '...' should generally contain a manipulator then yield
>> generates from where it stopped...*getting it*
>>
>
>
> It would help if you deleted the irrelevent bits so we can see
> which '...' you mean.
>
> I'm guessing it was this comment, right at the end, by Danny:
>
>
>      We _can_ write a loop to run over such infinite sequences, but we'll
>>     also have to make sure to stop it manually: it won't exhaust
>>     otherwise, so doing something like:
>>
>>     #################
>>     for n in onlyEvens():
>>          ...
>>     #################
>>
>>     better have something in the "..." that interrupts or returns, or else
>>     that loop will never end.
>>
>
>
> Notice that the ... here is not part of the generator.
> So the ... here references to how you process the output of the generator
> - the yielded values. So in this example case you'd
> have something like
>
>
> #################
> for n in onlyEvens():
>     if n > 1000: break     # prevent infinite loop
>     # now process the evens that we are interested in.
> #################
>
> The 'break' could also be a 'return' if the loop were
> inside a function.
>
> And it doesn't have to be a direct value check as shown the
> condition could be based on some external condition or even
> user input. The important point is to make sure that you
> have some way to exit the loop if the generator is infinite.
>
> hth
> --
> Alan G
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
> http://www.flickr.com/photos/alangauldphotos
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20140528/8bfe2ce6/attachment.html>


More information about the Tutor mailing list