python 为什么分配给 True/False 不像我期望的那样工作?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2055029/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 23:39:54  来源:igfitidea点击:

Why does assigning to True/False not work as I expect?

pythonpython-2.x

提问by paxdiablo

As part of answering another question, I wrote the following code whose behaviour seems bizarre at first glance:

作为回答另一个问题的一部分,我编写了以下代码,其行为乍一看似乎很奇怪:

print True                    # outputs true
True = False;    print True   # outputs false
True = True;     print True   # outputs false
True = not True; print True   # outputs true

Can anyone explain this strange behaviour? I think it has something to do with Python's object model but I'm not sure.

谁能解释这种奇怪的行为?我认为这与 Python 的对象模型有关,但我不确定。

It's version 2.5.2 under Cygwin.

它是 Cygwin 下的 2.5.2 版。

回答by balpha

Python has these two (among others) builtin objects. They are just objects; in the beginning, they don't have any names yet, but to know what we refer to, let's call them 0x600Dand 0xBAD.

Python 有这两个(以及其他)内置对象。它们只是对象;一开始,它们还没有任何名称,但要知道我们指的是什么,让我们称它们为0x600D0xBAD

Before starting to execute a Python (2.x) script, the name Truegets bound to the object 0x600D, and the name Falsegets bound to the object 0xBAD, so when the program refers to True, it looks at 0x600D.

在开始执行 Python (2.x) 脚本之前,名称True绑定到对象0x600D,名称False绑定到对象0xBAD,因此当程序引用 时True,它会查看0x600D.

Because 0x600Dand 0xBADknow that they are usually used by the names Trueand False, that's what they output when they get printed, i.e. the __str__method of 0x600Dreturns 'True'and so on.

因为0x600D并且0xBAD知道它们通常由名称True和使用False,这就是它们在打印时输出的内容,即返回__str__方法等。0x600D'True'

True = False

now binds the name Trueto a different object. From now on, both names Trueand Falserefer to the same object 0xBAD, which, when printed, outputs False.

现在将名称绑定True到不同的对象。从现在开始,这两个名称TrueFalse引用同一个 object 0xBAD,当打印时,输出False.

True = True

doesn't really do anything: It takes the object referred to by the name True, and binds the new (and old) name Trueto this object. Since (because of the previous step) Truerefers to 0xBADbefore this, it still refers to 0xBADafter this. Hence, printing still outputs False.

并没有真正做任何事情:它获取 name 引用的对象True,并将新(和旧)名称绑定True到该对象。由于(因为上一步)True指的是0xBAD在此之前,它仍然指的是0xBAD在此之后。因此,打印仍然输出False.

True = not True

first takes the object that the name Trueis bound to, which is 0xBAD. It gives this object to the notoperator. notdoesn't care (or know) what name is used here to refer to 0xBAD, it just knows that when given 0xBADit should return 0x600D. This return value is then given to the assignment operator =, binding the name Trueto this object.

首先获取名称True绑定的对象,即0xBAD. 它将此对象提供给not操作员。not不关心(或知道)这里使用什么名称来指代0xBAD,它只知道在给出0xBAD时应该返回0x600D。然后将此返回值提供给赋值运算符=,将名称绑定True到此对象。

Since the name Truenow once more refers to the object 0x600D, calling print Trueoutputs True, and the world is good again.

由于名称True现在再次引用 object 0x600D,调用print True输出True,世界又好了。

回答by James

Imagine this instead:

想象一下:

A = True
B = False

print A           # true
A = B;  print A   # false
A = A;  print A   # false, because A is still false from before
A = not A; print A # true, because A was false, so not A is true

The exact same thing is going on, but in your version it's confusing, because you don't expect that you can redefine True and False.

完全相同的事情正在发生,但是在您的版本中它令人困惑,因为您不希望可以重新定义 True 和 False。

回答by Ignacio Vazquez-Abrams

In 2.x, True and False are not keywords so it's possible to shadow the built-ins in this manner.

在 2.x 中,True 和 False 不是关键字,因此可以以这种方式隐藏内置函数。

回答by ghostdog74

You can check whether True/False is a keyword:

您可以检查 True/False 是否是关键字:

>>> import keyword
>>> keyword.iskeyword('True')
False

Since it's not (in my version), assigning True=False just means "True" is another "variable" name.

由于它不是(在我的版本中),因此分配 True=False 仅意味着“True”是另一个“变量”名称。

回答by Byte Commander

You could easily restore the original values using simple Boolean comparisons:

您可以使用简单的布尔比较轻松恢复原始值:

True = 1==1
False = 1==0

Or by converting integer literals to bools:

或者通过将整数文字转换为布尔值:

True = bool(1)  # actually every number except 0 works
False = bool(0)