2

I found a peculiar behavior while going through Python 3 data types especially string. If two strings a and b have the same value then a is b becomes True (Strings must not contain hyphen of course).

If:

>>> a = 'string_without_hyphen'
>>> b = 'string_without_hyphen'

Then:

>>> a is b
True
>>> a == b
True

But if:

>>> a = 'string-with-hyphen'
>>> b = 'string-with-hyphen'

Then,

>>> a is b
False
>>> a == b
True

which confused me.

Why is this happening?

optimistic_creeper
  • 2,739
  • 3
  • 23
  • 37

1 Answers1

6

Because moon rays and unicorns implementation details.

The is operator compares objects by identity, not by content.

The Python implementation you're using may or may not decide to reuse the same string object for both a and b, if it feels like it, since strings are immutable in Python. The same may or may not occur for integers (and in fact, this also happens with Java's Integers if they're sufficiently small).

The gist is: never use is unless you really do need identity (address) comparison; things may be weird. Use == instead.

AKX
  • 152,115
  • 15
  • 115
  • 172
  • See the dup I found. This happens because the code executed in the REPL. – DeepSpace Aug 30 '18 at 10:53
  • @DeepSpace Er, the accepted answer in the duplicate says this is caused by the string being a name string, not by REPL use. – AKX Aug 30 '18 at 10:54
  • 2
    As you said, implementation details. I suppose the rules for string interning are not set in stone. See for yourself: Compare the output of `a = 'string-with-hyphen' ; b = 'string-with-hyphen' ; print(a is b)` when it is executed "at once" from a py file, and when it is executed line by line in a REPL – DeepSpace Aug 30 '18 at 10:57