Toggle

Rustom Mody rustompmody at gmail.com
Thu Oct 9 04:34:30 CEST 2014


On Thursday, October 9, 2014 7:12:41 AM UTC+5:30, Ben Finney wrote:
> Seymore4Head writes:

> > I want to toggle between color="Red" and color="Blue"

> It's good to cultivate ongoing familiarity with the standard library

And language. In recent python3:

>>> class Color(Enum):
...   Red = 0
...   Blue = 1
... 
>>> Color.Red
<Color.Red: 0>
>>> print (Color.Red)
Color.Red

# Not sure what to make of that distinction...

>>> c=Color.Red
>>> c = Color.Blue if c==Color.Red else Color.Red
>>> c
<Color.Blue: 1>
>>> 

>>> # Better
>>> def toggle(c): return Color.Blue if c==Color.Red else Color.Red
... 
>>> toggle(c)
<Color.Blue: 1>
>>> toggle(c)
<Color.Blue: 1>

# which means the c has not changed



>>> toggle(toggle(c))
<Color.Red: 0>


[Yeah and like Steven I find the colour of 'color' colourless]



More information about the Python-list mailing list