In Python 2, we could reassign True
and False
(but not None
), but all three (True
, False
, and None
) were considered builtin variables. However, in Py3k all three were changed into keywords as per the docs.
From my own speculation, I could only guess that it was to prevent shenanigans like this which derive from the old True, False = False, True
prank. However, in Python 2.7.5, and perhaps before, statements such as None = 3
which reassigned None
raised SyntaxError: cannot assign to None
.
Semantically, I don't believe True
, False
, and None
are keywords, since they are at last semantically literals, which is what Java has done. I checked PEP 0 (the index) and I couldn't find a PEP explaining why they were changed.
Are there performance benefits or other reasons for making them keywords as opposed to literals or special-casing them like None
in python2?
Possibly because Python 2.6 not only allowed
True = False
but also allowed you to say funny things like:which would reset
True
toFalse
for the entire process. It can lead to really funny things happening:EDIT: As pointed out by Mike, the Python wiki also states the following under Core Language Changes:
For two reasons, mainly:
__builtin__.True = False
prank hidden on a random module. (as explayned by devnull)This was discussed some months ago on python-dev. Having tons of links to the definition of True would be annoying, contrary to links to e.g. the nonlocal or with statements doc.
And things I conclude why True and False will make things "even finer".
re-bound as a side effect of functions called within the loop.
It's really easy to change True, such as:
def True(): print True
There is really no good use case for letting user code rebind the built-in names None, True, and False, making them keywords has almost only pluses.
Make program has to look-up "True" in symbol-table at each step just to find True has value True is far from intuitive. (that is why 1 is faster than True.)
reference:
Beginning talk related to assigning to True and False:
some Aux Data:
PS: some number shows True/1: