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

2019-01-20 08:12发布

Possible Duplicate:
Why can’t Python handle true/false values as I expect?

Seems a stupid question, but why is the following statement in Python not explicitly forbidden?

>> True=False
>> True
False

How is True and False handled by Python interpreter?

4条回答
仙女界的扛把子
2楼-- · 2019-01-20 08:23

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.

查看更多
Rolldiameter
3楼-- · 2019-01-20 08:32

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.

查看更多
你好瞎i
4楼-- · 2019-01-20 08:41
>>> 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
查看更多
兄弟一词,经得起流年.
5楼-- · 2019-01-20 08:42

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

查看更多
登录 后发表回答