[Tutor] Generator function question?

John Clark clajo04 at mac.com
Sat May 7 14:22:21 CEST 2005


Ooops - sorry - I spoke too soon...

It's clear that your code is a more elegant implementation of
neverEndingStatus() but I am still having to initialize a global variable
and call .next() when I want to get the next value... So the generator
function has been cleaned up, but the client code interaction with the
function is still pretty much the same.

(Either that, or I am not following what you mean when you say:

>      As for how to access it, use a for loop (for i in
> neverEndingStatus()). xrange, for example, is a generator function.

Can you please provide an example of how my Test1() function changes based
on your suggestion?  (sorry to be dense)

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