开发者

Comparing strings using '==' and 'is' [duplicate]

This question already has answers here: Closed 12 years ago.

Possible Duplicates:

Types for which “is&rdquo开发者_如何学C; keyword may be equivalent to equality operator in Python

Python “is” operator behaves unexpectedly with integers

Hi.

I have a question which perhaps might enlighten me on more than what I am asking.

Consider this:

>>> x = 'Hello'
>>> y = 'Hello'
>>> x == y
True
>>> x is y
True

I have always used the comparison operator. Also I read that is compares the memory address and hence in this case, returns True

So my question is, is this another way to compare variables in Python? If yes, then why is this not used?

Also I noticed that in C++, if the variables have the same value, their memory addresses are different.

{ int x = 40; int y = 40; cout << &x, &y; }
0xbfe89638, 0xbfe89634

What is the reason for Python having the same memory addresses?


This is an implementation detail and absolutely not to be relied upon. is compares identities, not values. Short strings are interned, so they map to the same memory address, but this doesn't mean you should compare them with is. Stick to ==.


There are two ways to check for equality in Python: == and is. == will check the value, while is will check the identity. In almost every case, if is is true, then == must be true.

Sometimes, Python (specifically, CPython) will optimize values together so that they have the same identity. This is especially true for short strings. Python realizes that 'Hello' is the same as 'Hello' and since strings are immutable, they become the same through string interning / string pooling.

See a related question: Python: Why does ("hello" is "hello") evaluate as True?


This is because of a Python feature called String interning which is a method of storing only one copy of each distinct string value.


In Python both strings and integers are immutable therefore you can cache them. Integers in the range of ´-5´ to ´256´ and small strings(don't know the exact size atm) get cached, therefore they are the same object. x and y are only names that refer to these objects.

Also == compares for equals values, while is compares for object identity. None True and False are global objects, for example you can rebind False to True.

The following shows that not every thing is being cached:

x = 'Test' * 2000
y = 'Test' * 2000

>>> x == y
True
>>> x is y
False

>>> x = 10000000000000
>>> y = 10000000000000
>>> x == y
True
>>> x is y
False


In Python, variables are just names that point to some object (and they can point to the same object). In C++, variables also define the actual memory that is reserved for them; this is why they have distinct memory addresses.

About Python string interning and differences between the two comparison operators, see carl's response.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜