Best way to generate alternate toggling values in a loop?

Grant Edwards grante at visi.com
Thu Oct 18 09:09:24 EDT 2007


On 2007-10-18, Gerard Flanagan <grflanagan at yahoo.co.uk> wrote:
> On Oct 18, 1:55 am, Debajit Adhikary <debaj... at gmail.com> wrote:
>> I'm writing this little Python program which will pull values from a
>> database and generate some XHTML.
>>
>> I'm generating a <table> where I would like the alternate <tr>'s to be
>>
>> <tr class="Even">
>> and
>> <tr class="Odd">
>>
>> What is the best way to do this?
>>
>
>
> from itertools import izip
>
> def toggle(start=True):
>     flag = start
>     while 1:
>         flag = not flag
>         yield flag

I like the solution somebody sent me via PM:

def toggle():
    while 1:
        yield "Even"
        yield "Odd"

-- 
Grant Edwards                   grante             Yow! Are we THERE yet?
                                  at               
                               visi.com            



More information about the Python-list mailing list