So Im trying get mutability a bit better into my brain and I saw quite a lot of experienced people struggle sometimes.
I made this little test code here:
x = 1
def test():
x = 2
test()
print(x) #1
x = 1
def test():
global x
x = 2
test()
print(x) #2
x = [1]
def test():
x = [2]
test()
print(x) #[1]
x = [1]
def test():
global x
x = [2]
test()
print(x) # [2]
x = [1]
def test():
x[0] = 2
test()
print(x) #[2]
Everything is clear to me except what the difference between the last and second last could be. What exactly is the rule to this. I noticed that it is possible to change the value of the object but not the object type itself or did I understand this wrong?