Best way to generate alternate toggling values in a loop?

Grant Edwards grante at visi.com
Wed Oct 17 21:29:38 EDT 2007


On 2007-10-17, Debajit Adhikary <debajit1 at gmail.com> wrote:

> # Start of Code
>
> def evenOdd():
>     values = ["Even", "Odd"]
>     state = 0
>     while True:
>         yield values[state]
>         state = (state + 1) % 2

I'd replace the last line with
         
          state ^= 1

to save a couple instructions, but I spend too much time
working with micoroprocessors running on clocks measured in the
KHz.

There are probably other more Pythonic ways...

-- 
Grant Edwards                   grante             Yow!  My EARS are GONE!!
                                  at               
                               visi.com            



More information about the Python-list mailing list