5

Possible Duplicate:
Python ‘==’ vs ‘is’ comparing strings, ‘is’ fails sometimes, why?

Is

a == b

the same as

a is b

?

If not, what is the difference?

Edit: Why does

a = 1
a is 1

return True, but

a = 100.5
a is 100.5

return False?

Community
  • 1
  • 1
Matthew
  • 28,056
  • 26
  • 104
  • 170
  • 1
    dupe http://stackoverflow.com/questions/1504717/python-vs-is-comparing-strings-is-fails-sometimes-why/1504742#1504742 among many others – SilentGhost Oct 28 '09 at 00:00
  • Oops, sorry. That one didn't show up in the list when I asked this--feel free to close this. – Matthew Oct 28 '09 at 00:09

2 Answers2

13

No, these aren't the same. is is a check for object identity - ie, checking if a and b are exactly the same object. Example:

a = 100.5
a is 100.5  # => False
a == 100.5  # => True

a = [1,2,3]
b = [1,2,3]
a == b  # => True
a is b  # => False
a = b
a == b  # => True
a is b  # => True, because if we change a, b changes too.

So: use == if you mean the objects should represent the same thing (most common usage) and is if you mean the objects should be in identical pieces of memory (you'd know if you needed the latter).

Also, you can overload == via the __eq__ operator, but you can't overload is.

Peter
  • 127,331
  • 53
  • 180
  • 211
  • 3
    Java doesn't have `is`, so it's hard to say how this is the exact opposite of it. – Pavel Minaev Oct 28 '09 at 00:02
  • in java, `==` tests memory address – hasen Oct 28 '09 at 00:08
  • 1
    good answer, but doesn't answer the part about `a is 1` returning true. – hasen Oct 28 '09 at 00:10
  • 2
    yep, that was an edit after the fact. this is interpreter-dependent, and reflects the fact that small integers are cached for speed. thus things equal to `1` will point to the same address for performance reasons. you can't rely on this, though. – Peter Oct 28 '09 at 00:11
  • @hasen j I'm not sure about the underlying engine, but if `a = [1,2,3]` and `b = [1,2,3]`, then wouldn't `a == b` evaluate to true regardless of memory address? – Jeremy S Oct 28 '09 at 00:12
  • @Jeremy, in Java `a = [1,2,3]` is not valid code – hasen Oct 28 '09 at 00:17
  • @hasen j I was focusing more on the `==` operator; you could use `a=1; b=1` for syntactical correctness. I think I am simply misinterpreting your comment though. – Jeremy S Oct 28 '09 at 17:52
5

As already very clearly explained above.

is : used for identity testing (identical 'objects')

== : used for equality testing (~~ identical value)

Also keep in mind that Python uses string interning (as an optimisation) so you can get the following strange side-effects:

>>> a = "test"
>>> b = "test"
>>> a is b
True
>>> "test_string" is "test" + "_" + "string"
True

>>> a = 5; b = 6; c = 5; d = a
>>> d is a
True  # --> expected
>>> b is a
False # --> expected
>>> c is a
True  # --> unexpected
Community
  • 1
  • 1
ChristopheD
  • 112,638
  • 29
  • 165
  • 179
  • For numbers, it only works for integers that take on byte (e.g. `<= 256`) – hasen Oct 28 '09 at 00:11
  • And for strings, it doesn't "detect" similar strings magically. Consider: `>>> a = "test" >>> b = "test" >>> a is b True >>> a = "test_string" >>> c = b + "_string" >>> a == c True >>> a is c False ` – hasen Oct 28 '09 at 00:13
  • 2
    this is due to the length of the strings / integers, and is **interpreter dependent**: don't rely on this behaviour. – Peter Oct 28 '09 at 00:15