You can convert True and False to strings 'True' and 'False' with str() . Non-empty strings are considered True , so if you convert False to strings with str() and then back to bool type with bool() , it will be True .
In Math a = b = c mean all a = b , b = c and a = c . So True is False == False means True == False and False == False and True == False , which is False . For boolean constants, is is equivalent to == . Actually, a is b == c only means (a is b) and (b == c) : a and c are not compared.
In Python, the two Boolean values are True and False (the capitalization must be exactly as shown), and the Python type is bool. In the first statement, the two operands evaluate to equal values, so the expression evaluates to True; in the second statement, 5 is not equal to 6, so we get False.
You can check if a value is either truthy or falsy with the built-in bool() function. According to the Python Documentation, this function: Returns a Boolean value, i.e. one of True or False .
You can simply del
your custom name to set it back to the default:
>>> True = False
>>> True
False
>>> del True
>>> True
True
>>>
This works:
>>> True = False
>>> True
False
>>> True = not False
>>> True
True
but fails if False
has been fiddled with as well. Therefore this is better:
>>> True = not None
as None
cannot be reassigned.
These also evaluate to True
regardless of whether True
has been reassigned to False
, 5
, 'foo'
, None
, etc:
>>> True = True == True # fails if True = float('nan')
>>> True = True is True
>>> True = not True or not not True
>>> True = not not True if True else not True
>>> True = not 0
Another way:
>>> True = 1 == 1
>>> False = 1 == 2
For completeness: Kevin mentions that you could also fetch the real True
from __builtins__
:
>>> True = False
>>> True
False
>>> True = __builtins__.True
>>> True
True
But that True
can also be overriden:
>>> __builtins__.True = False
>>> __builtins__.True
False
So better to go with one of the other options.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With