How are tuples immutable?

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

How are tuples immutable?

Postby sc25893 » Sat Jun 29, 2013 8:11 pm

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 += veg

print(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?

Postby micseydel » Sat Jun 29, 2013 9:31 pm

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 one
True
>>> id(copy_one), id(one)
(140464556744528, 140464556744528)
>>>
>>> two = ('two',)
>>> one += two
>>> copy_one is one
False
>>> 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.
Join the #python-forum IRC channel on irc.freenode.net!
User avatar
micseydel
 
Posts: 941
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: How are tuples immutable?

Postby DrakeMagi » Sun Jun 30, 2013 3:44 am

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_function

fruits = ("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)
DrakeMagi
 
Posts: 72
Joined: Sun May 12, 2013 8:36 pm

Re: How are tuples immutable?

Postby sc25893 » Sun Jun 30, 2013 2:02 pm

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?

Postby XndrK » Fri Jul 26, 2013 7:33 pm

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_function

fruits = ("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 2.7.5

https://github.com/4of92000
https://github.com/PythonForum/
User avatar
XndrK
 
Posts: 131
Joined: Sat Jun 15, 2013 7:57 pm

Re: How are tuples immutable?

Postby Yoriz » Fri Jul 26, 2013 7:37 pm

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.
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 571
Joined: Fri Feb 08, 2013 1:35 am
Location: UK


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 1 guest