Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can I assign True = False (Python 2.7.9) [duplicate]

Tags:

python

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
>>>
like image 578
double_j Avatar asked Jun 30 '15 19:06

double_j


2 Answers

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.

like image 169
JasonFruit Avatar answered Nov 04 '22 20:11

JasonFruit


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

like image 29
James Avatar answered Nov 04 '22 19:11

James