Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #72471

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

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.albasani.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'python.': 0.02; '"this': 0.03; 'value,': 0.04; 'configure': 0.05; 'run-time': 0.05; 'assignment': 0.07; 'discard': 0.07; 'list?': 0.07; 'string': 0.09; 'exist,': 0.09; 'immutable': 0.09; 'objects)': 0.09; 'objects:': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'python': 0.11; 'creates': 0.14; 'question.': 0.14; 'times,': 0.14; 'bind': 0.16; 'concatenate': 0.16; 'container,': 0.16; 'distinct': 0.16; 'email name:ben': 0.16; 'finney': 0.16; 'garbage': 0.16; 'guess.': 0.16; 'helps.': 0.16; 'immutable,': 0.16; 'immutable;': 0.16; 'invisible': 0.16; 'item)': 0.16; 'object;': 0.16; 'objects.': 0.16; 'operation.': 0.16; 'programmer,': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'slight': 0.16; 'unavailable': 0.16; 'url:tutor': 0.16; 'value;': 0.16; 'bit': 0.19; 'message-----': 0.19; 'value.': 0.19; '(the': 0.22; '>>>': 0.22; 'memory': 0.22; 'creating': 0.23; 'header:User- Agent:1': 0.23; 'refers': 0.24; 'text.': 0.24; '(or': 0.24; 'question': 0.24; 'possibly': 0.26; 'references': 0.26; 'second': 0.26; 'asking': 0.27; 'values': 0.27; 'header:X-Complaints-To:1': 0.27; '(this': 0.29; 'subject:) ': 0.29; 'important.': 0.30; 'locations': 0.30; 'statement': 0.30; 'especially': 0.30; 'url:mailman': 0.30; 'included': 0.31; 'posting': 0.31; 'that.': 0.31; 'becoming': 0.31; 'changed.': 0.31; 'container': 0.31; 'flying': 0.31; 'object.': 0.31; 'values.': 0.31; 'writes:': 0.31; 'text': 0.33; 'url:python': 0.33; '(e.g.': 0.33; 'plain': 0.33; 'subject:the': 0.34; 'subject: (': 0.35; 'beyond': 0.35; 'objects': 0.35; 'operations': 0.35; 'add': 0.35; 'really': 0.36; 'largely': 0.36; 'object,': 0.36; 'patient': 0.36; 'url:listinfo': 0.36; 'thanks': 0.36; 'url:org': 0.36; 'half': 0.37; 'list': 0.37; 'clear': 0.37; 'ben': 0.38; 'follows:': 0.38; 'question,': 0.38; 'to:addr:python-list': 0.38; 'skip:- 10': 0.38; 'anything': 0.39; 'does': 0.39; 'subject:can': 0.39; 'to:addr:python.org': 0.39; 'changed': 0.39; 'enough': 0.39; 'received:org': 0.40; 'url:mail': 0.40; 'even': 0.60; 'subject:"': 0.60; 'hope': 0.61; 'free': 0.61; 'forum': 0.61; 'new': 0.61; "you're": 0.61; 'first': 0.61; 'discuss': 0.62; "you'll": 0.62; 'name': 0.63; 'kind': 0.63; 'refer': 0.63; 'our': 0.64; 'more': 0.64; 'different': 0.65; 'god': 0.65; 'skip:\xe2 10': 0.65; 'direct': 0.67; '8bit%:40': 0.68; 'body.': 0.68; 'forums': 0.68; '\xe2\x80\x93': 0.77; 'fairies': 0.84; 'irrelevant': 0.84; 'optimisation': 0.84; 'perspective.': 0.84; 'programs:': 0.84; 'received:125': 0.84; 'strategies,': 0.84; 'subject:being': 0.84; 'adopt': 0.91; 'good,': 0.91; 'journey': 0.93
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Ben Finney <ben@benfinney.id.au>
Subject Re: can someone explain the concept of "strings (or whatever) being immutable"
Date Tue, 03 Jun 2014 15:33:08 +1000
References <cb9e0aa79b1.00000b26codemonkey@inbox.com> <CBDAD7DCC2E.00000B3Ccodemonkey@inbox.com>
Mime-Version 1.0
Content-Type text/plain; charset=utf-8
Content-Transfer-Encoding 8bit
X-Gmane-NNTP-Posting-Host jigong.madmonks.org
X-Public-Key-ID 0xAC128405
X-Public-Key-Fingerprint 517C F14B B2F3 98B0 CB35 4855 B8B2 4C06 AC12 8405
X-Public-Key-URL http://www.benfinney.id.au/contact/bfinney-pubkey.asc
X-Post-From Ben Finney <bignose+hates-spam@benfinney.id.au>
User-Agent Gnus/5.13 (Gnus v5.13) Emacs/23.4 (gnu/linux)
Cancel-Lock sha1:IbfVwVwLBnGBXSOPeW/nMQkGiYI=
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.10593.1401773605.18130.python-list@python.org> (permalink)
Lines 122
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1401773605 news.xs4all.nl 2833 [2001:888:2000:d::a6]:38772
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:72471

Show key headers only | View raw


Deb Wyatt <codemonkey@inbox.com> writes:

> > -----Original Message-----
> > From: ben@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

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: can someone explain the concept of "strings (or whatever) being immutable" Ben Finney <ben@benfinney.id.au> - 2014-06-03 15:33 +1000

csiph-web