Jan-06-2019, 02:27 PM
more from the docs
In your example you change mutable object - list, within immutable tuple
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
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs