No, it says lists are mutable and tuples are immutable.  

Mutable has the same root as "mutation".  Mutable means "can be changed in 
place".   Immutable means "cannot be changed in place".

Examples: 
1) pass your list to a function, the function modifies the list. When the 
function returns your script gets control back. Your list is modified.
2) pass a tuple to a function.  The function wants to modify the tuple.  It 
can't Append( ) to it, if it tries Python will throw an exception because 
tuples don't have an append method.  It can't assign a new value to an element 
of the tuple.  But it can assign different content to the tuple entirely.  When 
the function returns your script gets control back. YOUR tuple is NOT modified. 
 When the function assigned to it, a new tuple with a different ID was created. 
 Basically, from that point on, the function had its own tuple.  The original 
was not modified because it can't be modified. It's immutable.
3) You use a list constructor, i.e., function( list(mylist) ).   That passes a 
copy of your list, which has a different ID, to the function.  The function can 
append to it, or otherwise modify it.  When the function returns, YOUR list is 
not modified because you didn't pass your list to the function; you passed a 
newly constructed copy of your list to the function.

(Advanced comment:  If a tuple has an element which is a list, that list is 
mutable.  Thinking about whether the list inside the tuple should be modifiable 
gives me a headache.  Try it and see what happens.)

If you don't like the terms mutable and immutable, think of them as related to  
"pass by reference" and "pass by value" in some other languages.  If you pass 
by reference, you give the function a reference to your object; it modifies 
your object.  If you pass by value, you load a copy of your object probably 
onto the stack, to pass to the function; the function can modify the copy but 
your object is not modified (this seems most similar to example 3 above). To 
avoid having to copy huge things onto the stack, C++ grew a const keyword so 
that you can pass a const reference to a function, which means the function 
won't be allowed to modify it.  I think this is very much like immutable.

I think it's fair to say Python always passes by reference.  Immutable types 
allow Python to have behavior that acts sort of like pass by value, or very 
much like passing a const reference in C++.  (One difference is Python allows a 
line that looks like it's assigning to the tuple, but really it makes a new 
tuple.)  In example 3 above Python didn't actually pass a copy of your list 
(which could be huge), it passed a reference to a copy of your list.  

--- Joseph Schachner

-----Original Message-----
From: Sharan Basappa <sharan.basa...@gmail.com> 
Sent: Wednesday, June 13, 2018 10:57 PM
To: python-list@python.org
Subject: mutable sequences

The term mutable appears quite often in Python.
Can anyone explain what is meant by mutable and immutable sequences.

For example, Python lists are mutable.

BTW, is the below explanation correct (it is taken from a book I am reading)

Python lists are mutable sequences. They are very similar to tuples, but they 
don't have the restrictions due to immutability.

It says lists are mutable and then says they are immutable???


-- 
https://mail.python.org/mailman/listinfo/python-list

Reply via email to