-2

In the code below When I reassign the value of name, this change is not reflected in the value of student

Can someone explain the behaviour of these variables?

I am more interested in the process that is saving the "snapshot" of the variable. If someone can, drop a technical term so i can do some digging thanks!

name = "jim"
student = name
name = "tim"

print(name) # returns tim
print(student) # returns jim
icecream
  • 31
  • 5
  • 1
    Assigning a value to a variable affects only *that variable*. The previous value of the variable plays no part in the process. – jasonharper Dec 19 '22 at 16:24
  • 1
    This isn't really a question about mutable vs immutable types; OP would see the same kind of result with `name = [1,2,3]; student = name; name = [4,5,6]`. (Not saying there isn't a suitable duplicate out there, but https://stackoverflow.com/questions/8056130/immutable-vs-mutable-types isn't it.) – chepner Dec 19 '22 at 16:26

1 Answers1

3

This has nothing to do with immutability. You assigned the current value of name to the name student; you did not "link" the name student to the name name.

See https://nedbatchelder.com/text/names.html, especially the third fact ("Names are reassigned independently of other names").

chepner
  • 497,756
  • 71
  • 530
  • 681