Best way to generate alternate toggling values in a loop?

cokofreedom at gmail.com cokofreedom at gmail.com
Thu Oct 18 10:09:24 EDT 2007


On Oct 18, 3:48 pm, Iain King <iaink... at gmail.com> wrote:
> On Oct 18, 2:29 am, Grant Edwards <gra... at visi.com> wrote:
>
>
>
> > On 2007-10-17, Debajit Adhikary <debaj... 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...
>
> I always use:
>
>            state = 1 - state
>
> for toggles.  I doubt it's much more pythonic though :)
>
> Iain

why not do
state = not state
?




More information about the Python-list mailing list