Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flip a boolean value without referencing it twice

I am looking for a short syntax that would look somewhat like x *= -1 where x is a number, but for booleans, if it even exists. It should behave like b = not(b). The interested of this is being able to flip a boolean in a single line when the variable name is very long.

For example, if you have a program where you can turn on|off lamps in a house, you want to avoid writing the full thing:

self.lamps_dict["kitchen"][1] = not self.lamps_dict["kitchen"][1]
like image 499
Guimoute Avatar asked Sep 02 '20 11:09

Guimoute


People also ask

How do you invert a Boolean value?

Invert a boolean method or propertyPress Ctrl+Shift+R and then choose Invert Boolean.

How do you make a boolean switch?

To toggle a boolean, use the strict inequality (! ==) operator to compare the boolean to true , e.g. bool !== true . The comparison will return false if the boolean value is equal to true and vice versa, effectively toggling the boolean.

How do you reverse a Boolean value in Python?

Use the not Operator to Negate a Boolean in Python The not operator in Python helps return the negative or the opposite value of a given boolean value. This operator is used by placing the not operator as a prefix of a given boolean expression. Check the example below. Here, the bool() function is used.

How do you represent boolean values?

Boolean values are True or False, 1 or 0. Use the words in all caps to represent Boolean values. Ex: TRUE; Use logical functions, like IF, OR, and AND, with Boolean values.

How to toggle a boolean value using JavaScript?

How to toggle a boolean using JavaScript ? A boolean value can be toggled in JavaScript by using two approaches which are discussed below: Method 1: Using the logical NOT operator: The logical NOT operator in Boolean algebra is used to negate an expression or value.

What is the reverse method of the Boolean test?

The reverse method compares the parameter b (which represents the user input boolean bn) using a conditional statement to the boolean value true. It returns the boolean value false if this condition is satisfied and returns true if it is not satisfied.

How do you return a boolean value from a string?

Boolean value false is returned for an input of String a (a=”true”) and vice versa. In the main method, String a is initialized to the value true and String b is initialized to the value false. Within the two print statements, the reverse method is called once per statement.


1 Answers

You can use xor operator (^):

x = True
x ^= True
print(x) # False
x ^= True
print(x) # True

Edit: As suggested by Guimoute in the comments, you can even shorten this by using x ^= 1 but it will change the type of x to an integer which might not be what you are looking for, although it will work without any problem where you use it as a condition directly, if x: or while x: etc.

like image 97
Asocia Avatar answered Sep 28 '22 06:09

Asocia