How are tuples immutable?

This is the place for queries that don't fit in any of the other categories.

How are tuples immutable?

I devised the following to try and get my head around it.

Code: Select all
`fruits = ("banana",           "apple",           "pear",           "mango")print(fruits)veg = ("potato",       "carrot",       "sweetcorn")print(veg)fruits += vegprint(fruits)print(veg)`

What I don't understand is, what happens to the original tuple if it is unchanged? Fruits had four items, now it has seven. Where has the tuple with four items gone if they can't be changed? Is a third tuple created from the first two? Pretty confused with it. I've read a number of explanations but none explain this so any extra input is appreciated, thanks.
sc25893

Posts: 23
Joined: Sat May 18, 2013 2:51 pm

Re: How are tuples immutable?

If you know C or some other low-level language, this should enlighten you
Code: Select all
`>>> one = ('one',)>>> copy_one = one>>> copy_one is oneTrue>>> id(copy_one), id(one)(140464556744528, 140464556744528)>>> >>> two = ('two',)>>> one += two>>> copy_one is oneFalse>>> id(one)140464556824984`

The original tuple, assuming you don't have another reference as I do above, gets garbage collected.

Honestly though, kudos for wanting to know, but my advice would be to go learn C, then come back to this question, if you're still not sure.
Due to the reasons discussed here we will be moving to python-forum.io on October 1, 2016.

This forum will be locked down and no one will be able to post/edit/create threads, etc. here from thereafter. Please create an account at the new site to continue discussion.

micseydel

Posts: 3000
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: How are tuples immutable?

tuples are immutable but not permanent or static. They are still dynamic.
python handle variables by pointing them to the data.

Code: Select all
`from __future__ import print_functionfruits = ("banana",           "apple",           "pear",           "mango")def print_fruits(fruit=fruits):    for f in fruit:        print(f)    print()veg = ("potato",       "carrot",       "sweetcorn")# here you dereference fruits data and create a new tuple.# if there no reference left. It destory the original.fruits += veg print_fruits() print_fruits(fruits)`
Linux: won't find windows here.
Linux: the choice of a GNU generation.
https://github.com/DrakeMagi
DrakeMagi

Posts: 112
Joined: Sun May 12, 2013 8:36 pm

Re: How are tuples immutable?

Thanks everyone for the replies, great explanations and crystal clear.
sc25893

Posts: 23
Joined: Sat May 18, 2013 2:51 pm

Re: How are tuples immutable?

DrakeMagi wrote:tuples are immutable but not permanent or static. They are still dynamic.
python handle variables by pointing them to the data.

Code: Select all
`from __future__ import print_functionfruits = ("banana",           "apple",           "pear",           "mango")def print_fruits(fruit=fruits):    for f in fruit:        print(f)    print()veg = ("potato",       "carrot",       "sweetcorn")# here you dereference fruits data and create a new tuple.# if there no reference left. It destory the original.fruits += veg print_fruits() print_fruits(fruits)`

In that case, what does "immutable" even mean?
Proverbs 26:14 describes me a bit too well.

Version: Python 3.4.3

https://github.com/4of92000
https://github.com/PythonForum/

XndrK

Posts: 208
Joined: Sat Jun 15, 2013 7:57 pm

Re: How are tuples immutable?

An object with a fixed value. Immutable objects include numbers, strings and tuples. Such an object cannot be altered. A new object has to be created if a different value has to be stored. They play an important role in places where a constant hash value is needed, for example as a key in a dictionary.
Due to the reasons discussed here we will be moving to python-forum.io/ on October 1 2016
This forum will be locked down and no one will be able to post/edit/create threads, etc. here from thereafter. Please create an account at the new site to continue discussion.

Yoriz

Posts: 1672
Joined: Fri Feb 08, 2013 1:35 am
Location: UK