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

Ben Finney ben at benfinney.id.au
Tue Jun 3 01:33:08 EDT 2014


Deb Wyatt <codemonkey at inbox.com> writes:

> > -----Original Message-----
> > From: ben at benfinney.id.au

> > Deb, can you expand a bit – and write the question in the body of
> > your message? It's not clear what you want explained.

> that's strange that you see no text.

A likely cause is that your message included no text body. An HTML-only
message will often be stripped out, because HTML in email is both
problematic and superfluous.

Best to configure your message composer to create plain text messages,
especially in forums like this where exact representation of program
text is important.

> The body of my email was as follows:

Thanks for posting in plain text this time.

> """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.

Good, that's half the journey there: you know that the first string is
not changed by that operation.

A slight tweak: it's better not to think about it in terms of
“location”, which is abstract and largely irrelevant in Python. Better
to think in terms of objects: the first string is one object, and the
second string is a distinct object.

> my question is what happens to the original string?? Is it still in
> memory somewhere, nameless?

That's a question whose answer is invisible from the perspective of the
Python programmer, who does not have direct access to memory locations
of Python objects.

Objects without references – a name is one kind of reference – cannot be
accessed, so may as well not exist from your perspective.

The run-time implementation of Python is free to discard unreferenced
objects at any time (this is known as “garbage collection”). Different
implementations will adopt different garbage collection strategies,
possibly multiple ones at different times, in order to clean up memory
in optimal ways.

>From our perspective writing Python programs: Once we no longer have a
reference to an object, it's unavailable forevermore.

> That was just the first question.  What does immutable really mean if
> you can add items to a list? and concatenate strings?

It means that an immutable object has a value which cannot be changed. I
don't know what “really mean” is asking beyond that.

    >>> foo = 8

The number 8 will always be 8, and that object will always have that
value. If you want ‘foo’ to refer to some other value, you'll need to
bind it to a different object, using an assignment statement or some
other re-binding operation.


A list is not immutable; you can mutate the list (e.g. by appending an
item) and the object remains the same object with a different value.

    >>> foo = ["spam", "eggs"]
    >>> foo.append("ham")
    >>> foo
    ["spam", "eggs", "ham"]

The list is a container, and the container remains the same object; the
items it contains (the list is a collection of references to objects)
can change without the list becoming a different object. Any reference
to the list will refer to a collection of different values over time,
depending on what mutations the list has undergone.


Concatenating strings creates a new string object; it does not change
the original string values.

    >>> foo = "Lorem ipsum"
    >>> bar = "dolor sit amet"
    >>> foo + ", " + bar
    'Lorem ipsum, dolor sit amet'
    >>> foo
    'Lorem ipsum'
    >>> bar
    'dolor sit amet'

The string objects are immutable, so they never change value; anything
which refers to the object will always refer to the same value.

Operations which produce different strings do so by creating new strings
(or re-producing the same strings if they already exist, which is an
optimisation the implementation is free to make).

> I don't understand enough to even ask a comprehensible question, I
> guess.

I hope that helps.

You're welcome to ask more questions in this forum.

You may also be interested to join the “tutor” forum
<URL:https://mail.python.org/mailman/listinfo/tutor>, where patient
tutors will discuss any beginner's Python questions.

-- 
 \           “I am as agnostic about God as I am about fairies and the |
  `\           Flying Spaghetti Monster.” —Richard Dawkins, 2006-10-13 |
_o__)                                                                  |
Ben Finney




More information about the Python-list mailing list