4

When I declare a variable in Python it references an object. In this case my object is the string 'test'. When I concatenate it with 'ing123', the variable a now points to a string object 'testing123'. However the reference count for 'testing123' does not increase. Why does this happen? I thought that python would simply rereference the previously existing 'testing123' string object. In this case it appears it's creating another object that getrefcount does not recognize. I guess my question is.... why does this occur? I was expecting the second call to getrefcount to return a 5 instead of a 4.

from sys import getrefcount

b = 'testing123'
print getrefcount('testing123')
a = 'test'
a += 'ing123'
print getrefcount('testing123')
print a

prints...

4
4
testing123
b10hazard
  • 7,399
  • 11
  • 40
  • 53
  • 2
    Why would the interpreter waste time trying to detect that a *computed* string already exists? – Cristian Ciupitu May 15 '13 at 19:09
  • 2
    c.f. http://stackoverflow.com/questions/11611750/under-which-circumstances-do-equal-strings-share-the-same-reference – Dave May 15 '13 at 19:13
  • @CristianCiupitu I don't know. That's why I asked. If you could elaborate on your comment, I'd be much obliged. – b10hazard May 15 '13 at 19:23
  • @Dave Thanks for the link. The top answer there seems to chalk it up to "The details of when strings are cached and reused are implementation-dependent, can change from Python version to Python version and cannot be relied upon" So it seems that an existing object is not always referenced. – b10hazard May 15 '13 at 19:26
  • @nater: there isn't much to elaborate, I was only trying to emphasize a possible reason for which that doesn't happen. – Cristian Ciupitu May 15 '13 at 19:33
  • @CristianCiupitu I think I see what you mean though. JanneKarila posted a link to an answer that expands on what you were saying. It boils down to not wasting time looking for existing objects because it will effect runtime speed, right? – b10hazard May 15 '13 at 19:38
  • Yes, that's how I see it, too. – Cristian Ciupitu May 15 '13 at 19:43

2 Answers2

3

intern the strings to get the behavior you want:

>>> b = intern('testing123')
>>> getrefcount(b)
2
>>> a = 'test'
>>> a = intern(a+'ing123')
>>> getrefcount(b)
3
>>> a is b
True
Cristian Ciupitu
  • 20,270
  • 7
  • 50
  • 76
Janne Karila
  • 24,266
  • 6
  • 53
  • 94
  • Doesn't really answer the question ("Why does python do this"), but useful nonetheless. +1 – Mark Hildreth May 15 '13 at 19:16
  • 1
    Here some thoughts on the why: http://stackoverflow.com/a/2124011/222914 – Janne Karila May 15 '13 at 19:26
  • @JanneKarila Thanks for the link. That is a very good explanation of why this is happening. I think this is what CristianCiupitu was talking about, if python tried to lookup a computed string in the memory each time, runtime speed would suffer. – b10hazard May 15 '13 at 19:35
  • The link you posted answers my question so I'll give you the correct answer. – b10hazard May 15 '13 at 19:42
-4

This is not recommended to do this.

Use dictionary for this, it´s more securely.

Try this

dict = { 'testing123' : 4 }
a = 'test'
a += 'ing123'
print dict[a]
Rodrigo
  • 188
  • 1
  • 8
  • 3
    You missed the point of this question; plus, redefining `dict` is not recommended. – Dave May 15 '13 at 19:15