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版本。

5 个解决方案

#1


73

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有这两个(以及其他)内置对象。它们只是物体;在开始时,它们还没有任何名称,但要知道我们所指的是什么,让我们称它们为0x600D和0xBAD。

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.

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

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.

因为0x600D和0xBAD知道它们通常被名称True和False使用,所以它们在打印时输出的是,即0x600D的__str__方法返回'True',依此类推。

True = 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和False都引用相同的对象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 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的对象,并将新(和旧)名称True绑定到此对象。由于(因为上一步)True在此之前指的是0xBAD,所以在此之后它仍然指的是0xBAD。因此,打印仍然输出False。

True = not 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绑定的对象,即0xBAD。它将此对象提供给not运算符。不关心(或知道)这里使用的是什么名称来引用0xBAD,它只知道当给定0xBAD时它应该返回0x600D。然后将此返回值赋给赋值运算符=,将名称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现在再一次引用对象0x600D,调用print True输出True,世界又好了。

更多相关文章

  1. Python:在类中定义对象
  2. 自动完成在VS代码和Python中的自动化对象
  3. 具有相同时区但不同的utcoffset()的Datetime对象
  4. Python 【面向对象(类)】 学习笔记
  5. 如何将json转换为对象?
  6. Python 全栈开发七 面向对象
  7. Python可执行对象——exec、eval、compile
  8. python-selenium-定位一组对象
  9. AttributeError:'Flask'对象没有属性'login_manager' - Login_Ma

随机推荐

  1. MySQL—嵌套select使用该表时如何从表中
  2. MySQL连接的计算顺序是什么?
  3. 项目实战7—Mysql实现企业级数据库主从复
  4. mysql值以数组格式转换为PHP数组
  5. MySQL多个连接到付款数据的日历表
  6. mysql5.6配置同步复制的新方法以及常见问
  7. 在mysql shell中显示没有表行的查询结果(
  8. SQL调优案例,MYSQL服务器CPU100%问题解决
  9. mysql中select列表可以有group列表中没有
  10. 确定SQL UPDATE是否更改了列的值