I searched for web pages and stack overflow problems but couldn't find the answer to this question. The observation I made is that in Python 2.7.3, if you assigned two variables to the same single character string, like
>>> a = 'a' >>> b = 'a' >>> c = ' ' >>> d = ' '
Then the variables will have the same link:
>>> a is b True >>> c is d True
This is also true for some longer lines:
>>> a = 'abc' >>> b = 'abc' >>> a is b True >>> ' ' is ' ' True >>> ' ' * 1 is ' ' * 1 True
However, there are many cases where the link is (unexpectedly) not used:
>>> a = 'ac' >>> b = 'ac' >>> a is b False >>> c = ' ' >>> d = ' ' >>> c is d False >>> ' ' * 2 is ' ' * 2 False
Can someone explain the reason for this?
I suspect there may be simplifications / replacements made by the interpreter and / or some caching mechanism that exploits the fact that python strings are immutable for optimization in some special cases, but what do I know? I tried to make deep copies of the strings using the str constructor and the copy.deepcopy function, but the strings are still incompatible with shared links.
The reason I am having problems with this is because I am checking the inequality of string references in some unit tests that I am writing for new-style python class cloning methods.
python string immutability reference
Erif89
source share