I started learning Python just today using Python 2.7 and I have a question here about global variables True and False:
It seems I can overwrite the value of True and False as this:
False = True
# now the value of variable False is also true.
True = False
# because the value of False is true, after this the value of True is still true.
if True(or False):
print 'xxxx'
else:
print 'yyyy'
Now wether we put True or we put False as the if condition, it always prints ‘xxxx’.
Then how to recover from that fault situation? I imagine that we could use something like:
True = 1==1
False = 1!=1
but that seems bit dodgy to me. Is there any better way to do that?
Thanks.
(Also, it seems in Python 3.3 this action is no longer allowed?)
The way to “recover” from this is to not let it happen.
That said, you can always use the
bool()type to accessTrueandFalseagain. (bool()always returns one of the two boolean singletons.)Example:
If this is a simple
True = 'something'binding, then what happens is that a new nameTrueis created in the current namespace–the__builtins__module is not altered. In this case you can simply delete (unbind) the “True” name in your namespace. Then Python will use the one defined in__builtins__again.None of this is possible in Python 3 because
TrueandFalseare no longer names (variables) but keywords.