Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Are tuples immutable?
#1
Hello,
I want to ask a question about tuples and their definition in python.
How I understand tuples considered immutable objects. But the next line of code shows that I can modify an item from the tuple:
t = tuple(1, [1, 2])
t[1][0] = 0    # works fine
By modifying an object / item in tuple I modify tuple itself.
So either I do not understand definition or definition is not correct?
Thanks.
Reply
#2
Python documentation:

Quote:The value of some objects can change. Objects whose value can change are said to be mutable; objects whose value is unchangeable once they are created are called immutable. (The value of an immutable container object that contains a reference to a mutable object can change when the latter’s value is changed; however the container is still considered immutable, because the collection of objects it contains cannot be changed. So, immutability is not strictly the same as having an unchangeable value, it is more subtle.) An object’s mutability is determined by its type; for instance, numbers, strings and tuples are immutable, while dictionaries and lists are mutable.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#3
more from the docs

Quote:It is not possible to assign to the individual items of a tuple, however it is possible to create tuples which contain mutable objects, such as lists.

Though tuples may seem similar to lists, they are often used in different situations and for different purposes. Tuples are immutable, and usually contain a heterogeneous sequence of elements that are accessed via unpacking (see later in this section) or indexing (or even by attribute in the case of namedtuples).

In your example you change mutable object - list, within immutable tuple

>>> spam = (1, [2, 3])
>>> id(spam[1])
139977080078024
>>> spam[1][1] = 4
>>> spam
(1, [2, 4])
>>> id(spam[1]) # still same object id
139977080078024
>>> spam[1] = 'eggs'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment
>>> 
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#4
Thanks for the links.
It's clear to me that Python do not consider size change of an object (not object containing references to other objects, but considering objects containing objects through references etc) mutable.
Reply
#5
(Jan-06-2019, 02:41 PM)python_user_n Wrote: Python do not consider size change of an object (not object containing references to other objects, but considering objects containing objects through references etc) mutable.

sorry, but I am not sure I fully understand that.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#6
(Jan-06-2019, 01:00 PM)python_user_n Wrote: By modifying an object / item in tuple I modify tuple itself.

I think this is your key misunderstanding. You are not modifying the tuple. The tuple in memory is completely unchanged. Glossing over implementation details, the tuple contained two things when created: an integer and a reference to (memory location of) a list. You changed the list by referencing it through the tuple. After that, the tuple is still the same. It contains the same integer, and the same memory location. What is at that memory location changed, but that did not involve any changes in what is stored in the tuple. It just looks like you did, because python shows you what is at the memory location instead of that memory location itself.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#7
(Jan-06-2019, 03:28 PM)ichabod801 Wrote:
(Jan-06-2019, 01:00 PM)python_user_n Wrote: By modifying an object / item in tuple I modify tuple itself.

I think this is your key misunderstanding. You are not modifying the tuple. The tuple in memory is completely unchanged. Glossing over implementation details, the tuple contained two things when created: an integer and a reference to (memory location of) a list. You changed the list by referencing it through the tuple. After that, the tuple is still the same. It contains the same integer, and the same memory location. What is at that memory location changed, but that did not involve any changes in what is stored in the tuple. It just looks like you did, because python shows you what is at the memory location instead of that memory location itself.

Thank you. But I understood that from the Python definition.
Where I disagree is in the case you transfer this object (a tuple) somewhere you can't just transfer the integer and reference. You must transfer the objects to which the tuple contain references. So your overall object size in this case depend on referenced objects, and not only tuple itself. If we modify them this will impact the size. And size is mutable and can be considerably different from initial object size.
That's how I see it.

Anyway thanks everyone for your time and answers. Cya in the next question ;)
Reply
#8
(Jan-06-2019, 04:24 PM)python_user_n Wrote: Where I disagree is in the case you transfer this object (a tuple) somewhere you can't just transfer the integer and reference.

What do you mean by transfer?
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#9
(Jan-06-2019, 04:39 PM)ichabod801 Wrote:
(Jan-06-2019, 04:24 PM)python_user_n Wrote: Where I disagree is in the case you transfer this object (a tuple) somewhere you can't just transfer the integer and reference.
What do you mean by transfer?
I think they think all copies are deep copies?

@python_user_n - that's not the case. Given a tuple containing a list, copying the tuple does not require making a copy of the list. This can be verified empirically by creating the tuple containing the list, copying the tuple, modifying the list via one of the tuples, then observing through the other tuple that that list changed as well.
Reply
#10
(Jan-06-2019, 04:39 PM)ichabod801 Wrote: What do you mean by transfer?
Network transfer (through serialization or another method).
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  immutable types Skaperen 2 2,065 Jul-09-2021, 01:00 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020