TypeError: tuple object does not support item assignment

created at 06-30-2021 views: 1

The code below prompt error

def change(tupleTest):
    tupleTest[0] = 2

tupleTest = (1, 2, 3)
change(tupleTest)
print(tupleTest)
TypeError: tuple object does not support item assignment

The solution is actually easier to understand. That is:

  • tuple is only readable, not writable, so when there is a problem with tuple assignment, there will be problems.

Generally, if you want to do operations similar to arrays in C/C++, it is best to use list.

Change the above example to the following code will not explode similar errors

def change(tupleTest):
    tupleTest[0] = 2

tupleTest = [1, 2, 3]
change(tupleTest)
print(tupleTest)
Please log in to leave a comment.