Aug-24-2019, 05:24 PM
Is there a method or something to easily flip a Boolean value from False to True or vice versa?
effective means to flip boolean values?
|
Aug-24-2019, 05:24 PM
Is there a method or something to easily flip a Boolean value from False to True or vice versa?
Aug-24-2019, 05:25 PM
You use
not to do that, obviously.
Aug-24-2019, 08:16 PM
ndc85430 idea expressed in code:
>>> spam = True >>> spam = not spam >>> spam False >>> spam = not spam >>> spam TrueAnother possibility is to use itertools.cycle(). This is useful, if there are more than two values to flip: >>> from itertools import cycle >>> switch = cycle([True, False]).__next__ >>> spam = switch() >>> spam True >>> spam = switch() >>> spam False >>> spam = switch() >>> spam True >>> switch = cycle(['ON', 'STANDBY', 'OFF']).__next__ >>> spam = switch() >>> spam 'ON' >>> spam = switch() >>> spam 'STANDBY' >>> spam = switch() >>> spam 'OFF' >>> spam = switch() >>> spam 'ON'
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Aug-25-2019, 03:58 PM
(Aug-24-2019, 08:16 PM)perfringo Wrote: ndc85430 idea expressed in code: Thank you! That looks super useful! |
|