savagejen's octopress blog

Github is the new facebook.

Python Boolean *features*

(Amatus) brought to my attention the fact that True can be set to False in python.

So, for instance, if you open up your python interpreter and do:

1
2
3
>>> True=False
>>> print True
False

So, True can be set to False. True can be set to None. False can be set to True. False can be set to None. Luckily setting None to anything will give you an assignment error. Why on earth setting True to False doesn’t give you an assignment error, I will never understand. I certainly don’t consider this to be “expected behavior”or anything.

After you get over the shock of realizing that python’s boolean values are not immutable, you might think “Ok, but why would I ever set True=False?” I have no idea! But check this out:

1
2
3
4
5
6
7
8
9
>>> something=True=False
>>> myVariable=something
>>> myOtherVariable=True
>>> print myVariable
False
>>> print myOtherVariable
False
>>> print True
False

So you can change the value of True or False on the same line as you set a variable and that redefinition will affect setting every boolean value following it.

(The PEP for bool is also really a bizarre and interesting read.)

Comments