[Tutor] Generator function question?

John Clark clajo04 at mac.com
Sat May 7 14:12:40 CEST 2005


Lightbulb!

It's clear that I don't yet fully understand the power of generator
functions... It never occurred to me to yield out of the middle of a loop,
and yet, seeing your code, it's obvious that that's one of the primary
purposes of "yield".

Thanks,
-jdc


On 5/7/05 8:00 AM, "Max Noel" <maxnoel_fr at yahoo.fr> wrote:

> 
>      This is (IMO) more elegant:
> 
> def neverEndingStatus():
>      index = 0
>      statusChars = ['|', '\\', '-', '/']
>      while True:
>          yield statusChars[index]
>          index = (index + 1) % 4
> 
> 
>      As for how to access it, use a for loop (for i in
> neverEndingStatus()). xrange, for example, is a generator function.
> 
> -- Max
> maxnoel_fr at yahoo dot fr -- ICQ #85274019
> "Look at you hacker... A pathetic creature of meat and bone, panting
> and sweating as you run through my corridors... How can you challenge
> a perfect, immortal machine?"
> 




More information about the Tutor mailing list