7

From my understanding, if a variable of an immutable type is assigned a value equal to another variable of the same immutable type, they should both be referencing the same object. I am using Python 2.7.6, don't know if this is a bug.

This behaves like I how understood:

x = 'ab'
y = 'ab'
id(x) == id(y)
True

However, by altering a character, this does not behave:

x = 'a#'
y = 'a#'
id(x) == id(y)
False

Strangely though, parallel assignment is very different!

x, y = 'a#','a#'
id(x) == id(y)
True

I do not understand this behavior.

ewong718
  • 143
  • 7
  • 3
    There's no guarantee that two distinct strings are the same object in memory. Sometimes Python decides to intern them, sometimes not. It's definitely not a bug. I'm not sure why it behaves exactly as it does though. – Anonymous Jul 23 '15 at 18:30
  • possible duplicate of [Python string interning](http://stackoverflow.com/questions/15541404/python-string-interning) – NightShadeQueen Jul 23 '15 at 19:21

1 Answers1

5

What you're talking about is known as string interning. This is an internal mechanism and there is no guarantee that two distinct strings would be stored in the same place in memory. This is not a bug so don't rely on such behavior. This is in the same general category as undefined behavior in C/C++.

You may be interested in this answer.

While I am able to replicate this behavior in the REPL, the comparison always returns true for me if I put the code in a file and then run it with the interpreter.

By the way there is a way to guarantee that the objects are the same though:

>>> x = intern('a#')
>>> y = intern('a#')
>>> x is y
True

More details on the subject can be found in this blog post.

Anonymous
  • 11,740
  • 3
  • 40
  • 50