7

Possible Duplicates:
Types for which “is” 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?

Community
  • 1
  • 1
user225312
  • 126,773
  • 69
  • 172
  • 181

5 Answers5

12

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 ==.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • Aah I see. I will stick to `==`, however I wanted to be sure what I was doing was correct. – user225312 Aug 04 '10 at 08:58
  • Whether short strings are interned or not is dependent upon the interpreter implementation you're using - you can't ever rely on it. – Nick Bastin Aug 04 '10 at 10:28
10

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?

Community
  • 1
  • 1
carl
  • 49,756
  • 17
  • 74
  • 82
8

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

codaddict
  • 445,704
  • 82
  • 492
  • 529
1

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
Ivo Wetzel
  • 46,459
  • 16
  • 98
  • 112
1

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.

Community
  • 1
  • 1
Messa
  • 24,321
  • 6
  • 68
  • 92