2

Why do I get differing results with these two examples of the is operator?

>>> a = "1234"
>>> b = "1234"
>>> a is b
True
>>> a = "12 34"
>>> b = "12 34"
>>> a is b
False

What exactly does is do here? I understand that it is supposed to compare memory addresses, but I don't see how that causes these results.

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
user873286
  • 7,799
  • 7
  • 30
  • 38
  • 1
    This is answered in quite a bit of detail here: http://stackoverflow.com/questions/306313/python-is-operator-behaves-unexpectedly-with-integers – Yuushi Jan 18 '12 at 02:41
  • here is another [http://stackoverflow.com/questions/2987958/how-is-the-is-keyword-implemented-in-python](http://stackoverflow.com/questions/2987958/how-is-the-is-keyword-implemented-in-python) – Matt Jan 18 '12 at 02:45

3 Answers3

3

is is not an equality operator. It checks to see if two variables refer to the same object. If you were to do this:

a = "12 34'
b = a

then a is b would be True, since they refer to the same object.

The cases you present are due to implementation details of the Python interpreter. Since strings are immutable, in some cases, creating two of the same string will yield references to the same object -- i.e., in your first case, the Python interpreter only creates a single copy of "1234", and a and b both refer to that object. In the second case, the interpreter creates two copies. This is due to the way the interpreter creates and handles strings, and, as an implementation detail, should not be relied upon.

mipadi
  • 398,885
  • 90
  • 523
  • 479
1

From the docs:

The operators is and is not test for object identity: x is y is true if and only if x and y are the same object. x is not y yields the inverse truth value.

The id function shows the difference here:

>>> a = "1234"
>>> b = "1234"
>>> a is b
True
>>> id(a)
140340938833512
>>> id(b)
140340938833512

>>> a = "12 34"
>>> b = "12 34"
>>> a is b
False
>>> id(a)
140340938833568
>>> id(b)
140340938833624
Rob Wouters
  • 15,797
  • 3
  • 42
  • 36
1

The is operator is used to identify when two objects are the same - that is: internally, they are stored in the same memory position.

Python implementations are free to optimize unmutable objects - such as strings, integers and other numbers, so that when one does instantiate identical objects, the previously existing is re-used. For example, in cPython (the standard implementation), the "n" first integers (I don't remember the actual threshold) are always kept as a single instance in memory, so "is" will always be "True" for small integers.

Of course the is operator is not meant to be used in this way. One should not care if a string or an integer is internally cached or not (although there is even a built-in function - intern - that hints the interpreter to cache a string or object in this way).

The idea of using is is to verify whether one object is actually the same one that you ind in another place. To consistently verify if objects are equal, one should always use the equality operator ==.

jsbueno
  • 99,910
  • 10
  • 151
  • 209