1. Python
def test_function(x, y):
x += 1
y.append(4)
def main():
a = 1
b = [2, 3]
test_function(a, b)
print("a =", a)
print("b =", b)
main()
Output:
a = 1
b = [2, 3, 4]
-
If you pass a mutable object into a method, the method gets a reference to that same object and you can mutate it to your heart’s delight, but if you rebind the reference in the method, the outer scope will know nothing about it, and after you’re done, the outer reference will still point at the original object.