Why were True and False changed to keywords in Python 3

Solution 1:

Possibly because Python 2.6 not only allowed True = False but also allowed you to say funny things like:

__builtin__.True = False

which would reset True to False for the entire process. It can lead to really funny things happening:

>>> import __builtin__
>>> __builtin__.True = False
>>> True
False
>>> False
False
>>> __builtin__.False = True
>>> True
False
>>> False
False

EDIT: As pointed out by Mike, the Python wiki also states the following under Core Language Changes:

  • Make True and False keywords.
    • Reason: make assignment to them impossible.

Solution 2:

For two reasons, mainly:

  1. So people don't do a __builtin__.True = False prank hidden on a random module. (as explayned by devnull)
  2. Because keywords are faster than global builtins. In Python 2.x, the interpreter would have to resolve those variables' values before using them, which is a bit slower than keywords. (see Why is if True slower than if 1?)

Solution 3:

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".

  1. 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

  2. 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.

  3. 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.)

    x=compile('while 1: foop()', '', 'exec')

    dis.dis(x)

          0 SETUP_LOOP              19 (to 22)
          3 JUMP_FORWARD             4 (to 10)
          6 JUMP_IF_FALSE           11 (to 20)
          9 POP_TOP
    >>   10 LOAD_NAME                0 (foop)
         13 CALL_FUNCTION            0
         16 POP_TOP
         17 JUMP_ABSOLUTE           10
    >>   20 POP_TOP
         21 POP_BLOCK
    >>   22 LOAD_CONST               1 (None)
         25 RETURN_VALUE
    

x=compile('while True: foop()', '', 'exec')

dis.dis(x)

          0 SETUP_LOOP              19 (to 22)
    >>    3 LOAD_NAME                0 (True)
          6 JUMP_IF_FALSE           11 (to 20)
          9 POP_TOP
         10 LOAD_NAME                1 (foop)
         13 CALL_FUNCTION            0
         16 POP_TOP
         17 JUMP_ABSOLUTE            3
    >>   20 POP_TOP
         21 POP_BLOCK
    >>   22 LOAD_CONST               0 (None)
         25 RETURN_VALUE

reference:

  • http://mail.python.org/pipermail/python-checkins/2011-September/107641.html
  • http://mail.python.org/pipermail/python-checkins/2012-January/110353.html
  • https://groups.google.com/forum/#!searchin/comp.lang.python/False$20True$20keyword/comp.lang.python/65B6qWUf8wI/HwQ5pwnW7woJ

Beginning talk related to assigning to True and False:

  • http://bugs.python.org/issue2349
  • https://groups.google.com/forum/comp.lang.python/65B6qWUf8wI/HwQ5pwnW7woJ

some Aux Data:

PS: some number shows True/1:

[alex@lancelot test]$ timeit.py -c -s'import itertools as it' 'c=it.count()'
'while True:' '  if c.next()>99: break'
10000 loops, best of 3: 91 usec per loop

[alex@lancelot test]$ timeit.py -c -s'import itertools as it' 'c=it.count()'
'while 1:' '  if c.next()>99: break'
10000 loops, best of 3: 76 usec per loop