[python基础] python 2与python 3的区别,一个关于对象的未知的坑

一个坑,不知道为什么,先记录一下吧

1 while loop != 0:
2     // 省略运算
3     print loop
4     if loop == 0:
5         print (id(loop))
6         print (id(0))

在python 2.7中的运行结果

// 省略loop非0的循环打印
0
38093112 34396768

在python 3.x中的运行结果

// 省略loop非0的循环打印
0
495494208
495494208

在寻中对loop进行了反复运算(非 -1),如模运算

在反复运算的过程中,loop的对象一直在改变,当loop == 0 的时候,在python 2中出现 loop == 0但loop的内存地址(id)不等于0的id

此时loop的值用int打印出来是 0,用float打印出来是 0.0

但 id(loop) is not id(0)

在python 3中没有遇到这个问题,同样的一段code,在3中运行到 loop == 0 后,id(loop) == id(0)

暂时还不知道是为什么,先记录下来

原文地址:https://www.cnblogs.com/Liubit/p/7668426.html