【发布时间】:2011-03-25 03:43:44
【问题描述】:
可能的重复:
Types for which “is” keyword may be equivalent to equality operator in Python
Python “is” operator behaves unexpectedly with integers
嗨。
我有一个问题可能比我所问的更能启发我。
考虑一下:
>>> x = 'Hello'
>>> y = 'Hello'
>>> x == y
True
>>> x is y
True
我一直使用比较运算符。我还读到is 比较内存地址,因此在这种情况下,返回True
所以我的问题是,这是在 Python 中比较变量的另一种方法吗?如果是,那么为什么不使用它?
我还注意到,在 C++ 中,如果变量具有相同的值,则它们的内存地址是不同的。
{ int x = 40; int y = 40; cout << &x, &y; }
0xbfe89638, 0xbfe89634
Python 内存地址相同的原因是什么?
【问题讨论】:
标签: python