-1

I entered the following at the command line:

>>>a = 25
>>>b = 50
>>>t = (a, b)
>>>t
(25, 50)
>>>a = 50
>>>t
(25, 50)
>>>t = (a, b)
>>>t
(50, 50)

Why do I have to reassign the tuple (a, b) to t to see the change in a's value?

Jacob
  • 1,560
  • 4
  • 19
  • 41
BruceM
  • 319
  • 1
  • 3
  • 8

5 Answers5

1

This has less to do with tuples and more to do with how assignment works in Python which copies vs references.

This works for other container types (lists) and plain variables.

>>> a = 2
>>> b = [1, a]
>>> a = 7
>>> b
[1, 2]
>>> c = 1
>>> d = c
>>> c = 2
>>> d
1
matthew.
  • 176
  • 5
  • No copying is done. It's just that `a` and the first element of the list are the same value. When `a` is reassigned, it merely means that `a` is referring to a different object. It doesn't change what the first element of the list is. – zondo Aug 23 '16 at 03:56
1

You can understand it in this way:-

>>> a =25
>>> b = 50
>>> id(a)
6070712
>>> t = (a, b)
>>> id(t[0])
6070712
>>> a = 50
>>> id(a)
6070412
# When you assigned a = 50, it created new object,
#id(t[0]) and a is not same now, so t is still (25, 50)

This happened because int is immutable, every time you assign a value to it, new object would be created.

Repeat same with mutable type(which can be modified in place)

>>> ls1 = [1,2]
>>> ls2 = [3,4]
>>> t = (ls1, ls2)
>>> ls1[0] = 23
>>> t
([23, 2], [3, 4])
>>> id(ls1)
54696136
>>> id(t[0])
54696136
#here t[0] and ls1 are same object because we could modify ls1 in place

I hope it would help.

AlokThakur
  • 3,599
  • 1
  • 19
  • 32
1

At first, the value in tuple in python can not be changed. You can only declare a new tuple.

a = 25 means a is a variable. However, the a in t belong to the tuple t. It does not have any relationship with variable a.

The second t = (a, b) as same as t = (50, 50)

Furthermore, you can use id(a) and id(t) to see the difference in your memory address.

Mangosteen
  • 181
  • 3
  • 10
0

If we do an assign action like a = b and b is immutable(number, string, tuple etc),assign will make a copy rather than make reference. As a result, modify to the b do not effect a.

For mutable situation:

>>> a = []
>>> b = 3
>>> c = (a, b)
>>> c
([], 3)
>>> a.append(1)
>>> c
((1], 3)
sting_roc
  • 233
  • 2
  • 15
0

It is simple. In Python, the names, such as a and b and t are not objects, they just point to objects. When you enter

>>> a = 25
>>> b = 50

Python sets the name a to point to an int object with value 25 and b to point to int object with value 50.

when you create a tuple with

>>> t = a, b

(no parenthesis required here!) you're telling Python that "please make a new tuple of 2 elements, the first position of which should point to the object that a now points to and the second position should point to the object that b now points to. Actually it would work similarly with a list, or set as well:

>>> l = [a, b]
>>> s = {a, b}

Now the next statement:

>>> a = 50

Means "now, set a to point to an int object with value of 50". The first element of the tuple still continues to point to 25. Actually all assignments to variables behave this way in Python, be the value in a mutable or not:

>>> a = [1, 2]
>>> b = [3, 4] 
>>> t = a, b
>>> a = [5, 6]
>>> t
([1, 2], [3, 4])

Even though a points to a mutable value, a list, then a, b means *make a new tuple with first element being the object that a points to at this very moment, and second element being the object that b points to at this very moment; and then a = [5, 6] means *create a new list ... and now make a point to it`. The variables (names) in Python indeed are not "boxes", but they're sort of signs that point to the values.