True=False assignment in Python 2.x [duplicate]

断了今生、忘了曾经 提交于 2019-11-28 13:19:27

True, just like str or any other builtin, is just a name that exists in the scope by default. You can rebind it like any other such name.

Python actually has very few reserved words. All the rest are subject to redefinition. It's up to you to be careful!

>>> True = False
False

In the above assignment, True is just a variable like any other variable you use. Its scope is limited to the current scope. So you can assign any values to it like in the below example. Note that the comparison 2 < 3 still prints True, because you have still access to builtin.

>>> True = 3
>>> True
3
>>> 2 < 3
True

Typing

True = False

you create a new variable called True, which value you assign to False.

Answering your second question, True and False are customized versions of integers 1 and 0 (technically speaking, subclasses), which just have a different string representation.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!