Tuples and immutability

Mark Lawrence breamoreboy at yahoo.co.uk
Sun Mar 2 15:05:35 CET 2014

On 02/03/2014 13:38, Eric Jacoboni wrote:
> Le 02/03/2014 13:32, Ian Kelly a écrit :
>> On Sat, Mar 1, 2014 at 7:04 PM, Eric Jacoboni <eric.jacoboni at gmail.com> wrote:
>>> In fact, i think i'm gonna forget += on lists :)
>> Well, do what you want, but I think you're taking the wrong lesson
>> from this.  Don't forget about using += on lists.  Instead, forget
>> about using assignments, augmented or otherwise, on tuple elements.
>> Would you expect this to work?
> Well, the thing about += on lists that makes me forget it, like i said
> in my previous post, is that its behaviour is not consistent with +.

The behaviour is consistent except when you try to modify a tuple.

> Don't get me wrong: i don't expect that modifying a tuple element works.
> That's exactly my point: my initial question was, why it "half works :
> it should not work at all". I was thinking that += on lists worked like
> update() or extend() : modifying lists in place... It was my mistake

I'd like to see you get update to work on a list.  This shows how 
confused you are over this issue.

> So, yes, i still don't get the point using a += operation, which is not
> even consistent with the + operation (see my exemple on "spam" in my
> previous post). The + operator to return the modified list and the
> update() or extend() methods to do in place replacements are well enough
> for my present needs. Maybe, in the future, i will find a use case of +=
> for lists which is not covered by others methods, though... Who knows? I
> don't doubt that Python designers have made this choice and this
> behavior for a good reason: i've just not get it yet.

All of this comes about because of the known issue with tuples.  Take 
them out of the equation and the behaviour of =, +=, extend and append 
is consistent.

My fellow Pythonistas, ask not what our language can do for you, ask 
what you can do for our language.

Mark Lawrence

This email is free from viruses and malware because avast! Antivirus protection is active.

More information about the Python-list mailing list