13

How does the is operator determine if two objects are the same? How does it work? I can't find it documented.

MattDMo
  • 100,794
  • 21
  • 241
  • 231
bodacydo
  • 75,521
  • 93
  • 229
  • 319
  • It is similar to comparing pointers in C, or using `==` between objects in Java. – Felix Kling Mar 13 '10 at 14:35
  • S.Lott: Sorry, I am not an expert on documentation systems, I was unable to find precise meaning after reading through tens of documents. – bodacydo Mar 13 '10 at 19:48

3 Answers3

14

From the documentation:

Every object has an identity, a type and a value. An object’s identity never changes once it has been created; you may think of it as the object’s address in memory. The ‘is‘ operator compares the identity of two objects; the id() function returns an integer representing its identity (currently implemented as its address).

This would seem to indicate that it compares the memory addresses of the arguments, though the fact that it says "you may think of it as the object's address in memory" might indicate that the particular implementation is not guranteed; only the semantics are.

danben
  • 80,905
  • 18
  • 123
  • 145
  • In particular, I would assume that small integers (which have the same id) may not actually be stored anywhere specific in memory. – Andrew Jaffe Mar 15 '10 at 07:57
  • CPython compares the memory addresses, however that is an implementation detail like the value returned by id(). When i'm already mentioning id() it should be said that calls to id() on implementations with a moving GC can be very expensive, so one should avoid calling it. – DasIch Mar 15 '10 at 22:52
14

Comparison Operators

Is works by comparing the object referenced to see if the operands point to the same object.

>>> a = [1, 2]
>>> b = a
>>> a is b
True
>>> c = [1, 2]
>>> a is c
False

c is not the same list as a therefore the is relation is false.

msw
  • 42,753
  • 9
  • 87
  • 112
  • 4
    But! Be aware that this *can* be different for certain immutable objects. For example, a = 5; b = 5; a is b returns True, at this moment, on this laptop, on this version of Python. You can't guarantee that two objects will be different just because they were initialized separately. – Kirk Strauser Mar 13 '10 at 17:33
  • 1
    Indeed, the almost sole application of `is` in Python is for comparing singletons, like `if foo is None` or `sentinel = object() ... if bar is sentinel`. – Mike Graham Mar 13 '10 at 18:03
  • @Just: You *can* guarantee that some objects are different when separately initialized, as this answer has with lists, because you *must* know if `a.append(v)` will modify *c*. –  Mar 15 '10 at 07:56
  • @Roger: What I meant was that you don't always know that Python will yield two distinct objects just because they were initialized separately. That alone is not enough to guarantee distinction. I wanted to give a counterexample to msw's example, where they *were* different, showing that it's not always the case. Yes, you must use *is* to to verify distinctness if that's important to you, which was kind of my point. :-) – Kirk Strauser Mar 15 '10 at 15:36
7

To add to the other answers, you can think of a is b working as if it was is_(a, b):

def is_(a, b):
  return id(a) == id(b)

Note that you cannot directly replace a is b with id(a) == id(b), but the above function avoids that through parameters.

  • 2
    Just stumbled upon this: do you know why direct replacement does not work, but via function it is ok? – LOST Feb 22 '18 at 20:31