In Python 2, we could reassign True
and False
(but not None
), but all three (True
, False
, and None
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:
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.)
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
[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