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
>>>
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.
In python 3.x, True
and False
are reserved words
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With