Roy Smith <[EMAIL PROTECTED]> wrote:
> All of those just move around pointers to the same (interned) string.
Correct about the pointers, but the string is not interned:
Steven D'Aprano <[EMAIL PROTECTED]> wrote:
>>>> s1 = foo()
>>>> s2 = foo()
>>>> s1 == s2, s1 is s2
> (True, True)
>
> So the string "This is a test" within foo is not copied each time the
> function is called. However, the string "This is a test" is duplicated
> between foo and foo2 (the two functions don't share the same string
> instance):
>
>>>> s3 = foo2()
>>>> s3 == s1, s3 is s1
> (True, False)
>
In this specific example the two functions don't share the same string, but
that won't always be the case: if the string had been interned this would
have printed (True, True).
e.g. Removing all the spaces from the string produces a string which is
interned.
>>> def foo():
s = "Thisisatest"
return s
>>> def foo2():
return "Thisisatest"
>>> s1 = foo()
>>> s2 = foo2()
>>> s1 is s2
True
Bottom line, never make assumptions about when two literal strings will
share the same object and when they won't.
--
http://mail.python.org/mailman/listinfo/python-list