is operator in Python

Question:

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.

Asked By: user873286

||

Answers:

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.

Answered By: mipadi

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
Answered By: Rob Wouters

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

Answered By: jsbueno
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.