为什么分配给 True/False 不像我期望的那样工作? [英] Why does assigning to True/False not work as I expect?

查看:41
本文介绍了为什么分配给 True/False 不像我期望的那样工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

它是 Cygwin 下的 2.5.2 版.

It's version 2.5.2 under Cygwin.

推荐答案

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

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 0x600D and 0xBAD.

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

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

因为 0x600D0xBAD 知道它们通常以名称 TrueFalse 使用,这就是它们在打印时输出,即 0x600D__str__ 方法返回 'True' 等等.

Because 0x600D and 0xBAD know that they are usually used by the names True and False, that's what they output when they get printed, i.e. the __str__ method of 0x600D returns 'True' and so on.

True = False

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

now binds the name True to a different object. From now on, both names True and False refer to the same object 0xBAD, which, when printed, outputs False.

True = True

实际上并没有做任何事情:它接受名称为 True 的对象,并将新(和旧)名称 True 绑定到该对象.由于(因为上一步)True在此之前指的是0xBAD,所以在此之后仍指的是0xBAD.因此,打印仍然输出False.

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

True = not True

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

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

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

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

这篇关于为什么分配给 True/False 不像我期望的那样工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆