![]() |
effective means to flip boolean values? - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: effective means to flip boolean values? (/thread-20663.html) |
effective means to flip boolean values? - Exsul - Aug-24-2019 Is there a method or something to easily flip a Boolean value from False to True or vice versa? RE: effective means to flip boolean values? - ndc85430 - Aug-24-2019 You use not to do that, obviously.
RE: effective means to flip boolean values? - perfringo - Aug-24-2019 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' RE: effective means to flip boolean values? - Exsul - Aug-25-2019 (Aug-24-2019, 08:16 PM)perfringo Wrote: ndc85430 idea expressed in code: Thank you! That looks super useful! |