Toggle

Antoon Pardon antoon.pardon at rece.vub.ac.be
Fri Oct 10 13:44:08 CEST 2014


Op 09-10-14 om 03:42 schreef Ben Finney:
> Seymore4Head <Seymore4Head at Hotmail.invalid> writes:
>
>> I want to toggle between color="Red" and color="Blue"
> It's good to cultivate ongoing familiarity with the standard library
> <URL:https://docs.python.org/3/library/itertools.html#itertools.cycle>
> so that you can make use of wheels already invented and maintained::
>
>     import itertools
>
>     colours = ["Red", "Blue"]
>     colour_cycle = itertools.cycle(colours)
>
>     next(colour_cycle)    # → "Red"
>     next(colour_cycle)    # → "Blue"
>     next(colour_cycle)    # → "Red"
>     next(colour_cycle)    # → "Blue"
>     next(colour_cycle)    # → "Red"
>
>     for colour in colour_cycle:
>         # … loops indefinitely
>         # with ‘colour’ bound to each value in turn …

I think this solution will become rather involved fast, when
you have multiple toggles.

-- 
Antoon Pardon





More information about the Python-list mailing list