5

Why am I able to assign the Python keyword True to equal the Python keyword False using Python 2.7.9?

Python 2.7.9 (v2.7.9:648dcafa7e5f, Dec 10 2014, 10:10:46)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
>>> True
True
>>> True = False
>>> True
False
>>>

But when switching over to Python 3.4.3:

Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 23 2015, 02:52:03)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
>>> True = False
  File "<stdin>", line 1
SyntaxError: can't assign to keyword
>>>
double_j
  • 1,636
  • 1
  • 18
  • 27

3 Answers3

9

True and False were builtins in Python 2, but in Python 3 they are keywords — thus the error message. Strictly speaking, you're not assigning to them but shadowing them — which you can't do with a keyword.

JasonFruit
  • 7,764
  • 5
  • 46
  • 61
2

In python 3.x, True and False are reserved words

James
  • 1,198
  • 8
  • 14
  • 2
    They know that, they pointed that out in their 2nd example. Re-read the question. – Cory Kramer Jun 30 '15 at 19:07
  • 1
    @CoryKramer, Sorry if my answer wasn't clear. You can do this in python 2.x because True and False are not [reserved words](https://docs.python.org/2.5/ref/keywords.html). – James Jun 30 '15 at 19:09
2

Because in Python 3.X it's a keyword, and in 2.7.X it's a variable (as True=4869 works too, as well as False=[4,8,6,9])

ForceBru
  • 43,482
  • 10
  • 63
  • 98