-3

This might be a stupid question, but what exactly is the is function, and when would one use it?

From the context, i guess i could infer that it's equivalent to ==; but if that's the case, why have both? The Built-in Functions Reference shows nothing, and help(is) returns a SyntaxError.

Noob Saibot
  • 4,573
  • 10
  • 36
  • 60
  • 2
    `x is y` if and only if `x` and `y` holds the same object. – Elazar Jun 30 '13 at 19:02
  • Isn't `x == y` the same thing? Why have both? – Noob Saibot Jun 30 '13 at 19:02
  • No, because you could have two objects with the same values, but they wouldn't be the *same* objects. Suppose you and I live in the same apartment complex. Our apartments might be absolutely identical in every way (`my_apartment == your_apartment`), but they're two different places (`my_apartment is not your_apartment`). – Kirk Strauser Jun 30 '13 at 19:15

1 Answers1

3

is checks if the objects have the same identity. == only checks if they are equal.

>>> L1 = [1,2,3]
>>> L2 = [1,2,3]
>>> L1 is L2
False
>>> L1 == L2
True
michaelmeyer
  • 7,985
  • 7
  • 30
  • 36