6

Possible Duplicate:
String comparison in Python: is vs. ==
Python string interning
Why does comparing strings in Python using either '==' or 'is' sometimes produce a different result?

I used accidentally is and == for strings interchangeably, but I discovered is not always the same.

>>> Folder = "locales/"
>>> Folder2 = "locales/"
>>> Folder is Folder2
False
>>> Folder == Folder2
True
>>> File = "file"
>>> File2 = "file"
>>> File is File2
True
>>> File == File2
True
>>> 

Why in one case operators are interchangeable and in the other not ?

Community
  • 1
  • 1
Eduard Florinescu
  • 16,747
  • 28
  • 113
  • 179
  • Get out of this habit NOW! Comparing strings with `is` is not reliable, and non-portable across Python platforms (CPython/jython/Iron Python). Compare strings with `==`. Save `is` for identity comparison with singletons (like `None`) or objects of your own creation. Do you use `is` for comparing ints too? That is just as unreliable. – PaulMcG Oct 03 '12 at 10:05
  • @PaulMcGuire I didn't used this but discovered this by accident. And then tried it and discovered the above inconsistency. – Eduard Florinescu Oct 03 '12 at 10:29
  • 1
    Other relevant questions that could be considered dupe targets: [Why does comparing strings in Python using either '==' or 'is' sometimes produce a different result?](http://stackoverflow.com/q/1504717) and [Python string interning](http://stackoverflow.com/q/15541404) – Martijn Pieters Oct 29 '13 at 12:19

3 Answers3

12

Short strings are interned for efficiency, so will refer to the same object therefore is will be true.

This is an implementation detail in CPython, and is absolutely not to be relied on.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
4

This question sheds more light on it: String comparison in Python: is vs. ==

The short answer is: == tests for equal value where is tests for equal identity (via object reference).

The fact that 2 strings with equal value have equal identity suggests the python interpreter is optimizing, as Daniel Roseman confirms :)

Community
  • 1
  • 1
Morten Jensen
  • 5,818
  • 3
  • 43
  • 55
3

The == operator calls the internal __cmp__() method of the first object which compares it to the second one. This is true to all Python objects including strings. The is operator compares the objects identity:

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

E.g.:

s1 = 'abc'
id(s1) 
>>> 140058541968080
s2 = 'abc'
id(s2)
>>> 140058541968080

# The values the same due to SPECIFIC CPython behvaiour which detects 
# that the value exists in interpreter's memory thus there is no need
# to store it twice. 

s1 is s2
>>> True

s2 = 'cde'
id(s2)
>>> 140058541968040
s1 is s2
>>> False    
Zaur Nasibov
  • 22,280
  • 12
  • 56
  • 83