Posts: 17
Threads: 4
Joined: Jan 2019
Jan-06-2019, 01:00 PM
(This post was last modified: Jan-06-2019, 01:00 PM by python_user_n.)
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.
Posts: 1,936
Threads: 8
Joined: Jun 2018
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.
Posts: 8,089
Threads: 154
Joined: Sep 2016
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
>>>
Posts: 17
Threads: 4
Joined: Jan 2019
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.
Posts: 8,089
Threads: 154
Joined: Sep 2016
(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.
Posts: 4,229
Threads: 97
Joined: Sep 2016
(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.
Posts: 17
Threads: 4
Joined: Jan 2019
(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 ;)
Posts: 4,229
Threads: 97
Joined: Sep 2016
(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?
Posts: 2,344
Threads: 62
Joined: Sep 2016
(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.
Posts: 17
Threads: 4
Joined: Jan 2019
(Jan-06-2019, 04:39 PM)ichabod801 Wrote: What do you mean by transfer? Network transfer (through serialization or another method).
|