Python - What is the output: pass mutable value to function?

Question

What is the output of the following code?

def func(a, b, c): a = 2; b[0] = 'x'; c['a'] = 'y' 

l=1
m=[1]
n={'a':0} 
func(l, m,  n) 
print(l, m, n )


Click to view the answer

(1, ['x'], {'a': 'y'})

Note

The first assignment in the function doesn't impact the caller.

The second two do because they change passed-in mutable objects in place.

Related Quiz