为什么我可以指定True=False(Python 2.7.9)



为什么我可以使用Python 2.7.9将Python关键字True赋值为等于Python关键字False

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

但当切换到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和False是Python 2中的内置项,但在Python 3中它们是关键字,因此会出现错误消息。严格地说,你不是给它们赋值,而是给它们加阴影——这是用关键字不能做到的。

在python 3.x中,TrueFalse是保留字

因为在Python 3.X中它是一个关键字,在2.7.X中它是变量(True=4869False=[4,8,6,9]也一样)

最新更新