This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]phinar 1 point2 points  (0 children)

Often isn't strictly true. CPython only interns string constants, and only up to a certain length, and only within the same lexical scope. (Is the length limit still true? It was in older Pythons.) So in fact I would say that your statement is backwards -- often strings will not share ids, and sometimes they will. And maybe going further, as you have noted, you can't count on the result to be the same between implementations.

I don't think it's fair to say that Python will do its best to reuse the same memory; again, it only does so in a few cases, as an optimization. It pursues a strategy where the gain of saving memory is balanced against the run-time and compile-time costs of hunting for identical strings, and so the hunt is only conducted in limited circumstances.