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

prashanth B.G prash.bg at gmail.com
Tue Jun 3 01:20:53 EDT 2014


Hi Deb,

          Immutability means that once "This is a string" is created in
memory , the string cannot be changed. When we assign a_string with "A
different string" this "A different string" is in a new memory location
again (a new object) . "This is a string" and "A different string" are two
different objects which are in existence which have been assigned to the
variable a_string at different points in time.

          Now if  "This is a string" is no longer referenced in the scope
of the program , the reference count drops to zero at which point it is
taken out of memory. Even though we perform a string concatenation and
change the string from "A different string" to "A different string appended
with some more words" , a new object is created , the initial string "A
different string" is copied to this newly created object, "appended with
some more words" are added to arrive at the final "A different string
appended with some more words". The original "A different string" still
remains until the point wherein it is not longer referenced after which it
is cleaned up.

Thanks.


On Tue, Jun 3, 2014 at 10:36 AM, Deb Wyatt <codemonkey at inbox.com> wrote:

>
>
> > -----Original Message-----
> > From: ben at benfinney.id.au
> > Sent: Tue, 03 Jun 2014 14:54:01 +1000
> > To: python-list at python.org
> > Subject: Re: can someone explain the concept of "strings (or whatever)
> > being immutable"
> >
> > Deb Wyatt <codemonkey at inbox.com> writes:
> >
> >> [no text]
> >
> > Deb, can you expand a bit – and write the question in the body of your
> > message? It's not clear what you want explained.
> >
> > --
> >  \          “I hope if dogs ever take over the world, and they chose a |
> >   `\    king, they don't just go by size, because I bet there are some |
> > _o__)                   Chihuahuas with some good ideas.” —Jack Handey |
> > Ben Finney
> >
> > --
> > https://mail.python.org/mailman/listinfo/python-list
> that's strange that you see no text.  The body of my email was as follows:
>
> """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?
> """
> That was just the first question.  What does immutable really mean if you
> can add items to a list? and concatenate strings?  I don't understand
> enough to even ask a comprehensible question, I guess.
>
>
> Thanks in advance,
> Deb in WA, USA
>
> ____________________________________________________________
> FREE 3D EARTH SCREENSAVER - Watch the Earth right on your desktop!
> Check it out at http://www.inbox.com/earth
>
>
> --
> https://mail.python.org/mailman/listinfo/python-list
>



-- 
             *****HAVE A NICE DAY *****

                         Prashanth
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20140603/f57f9403/attachment.html>


More information about the Python-list mailing list