Why is there strange behavior between equivalent string identifiers?

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 refer to the same object. I am using Python 2.7.6, I do not know if this is a bug.

This behaves as I understood:

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

However, changing the symbol does not behave:

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

Strange though concurrent assignment is very different!

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

I do not understand this behavior.

+7
python
source share
1 answer

What you are talking about is called string interning . This is an internal mechanism, and there is no guarantee that two different lines will be stored in one place in memory. This is not a mistake, so do not rely on this behavior. This is in the same general category as undefined behavior in C / C ++.

You might be interested in this answer .

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

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

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

More on this can be found in this blog post .

+5
source share

All Articles