can someone explain the concept of "strings (or whatever) being immutable"

Ian Kelly ian.g.kelly at gmail.com
Tue Jun 3 00:51:05 EDT 2014


On Jun 2, 2014 10:41 PM, "Deb Wyatt" <codemonkey at inbox.com> wrote:
>
> a_string = "This is a string"
> a_string is pointing to the above string
>
> now I change the value of a_string
> a_string = "This string is different"
> I understand that now a_string is pointing to a different string than it
was before, in a different location.
>
> my question is what happens to the original string??  Is it still in
memory somewhere, nameless?
> Thanks in advance,

If there are still any references being held to the original string, then
it will remain in memory. Otherwise, the memory used for it will be freed.
You can't rely on this happening immediately (although in CPython it does,
as long as the object is not part of a reference cycle), but it will happen
eventually.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20140602/d1d36852/attachment.html>


More information about the Python-list mailing list