Mega Code Archive

 
Categories / Python / Function
 

Pass immutable and mutable value into function

def changer(x, y):          x = 2               # changes local name's value only     y[0] = 'spam'       # changes shared object in place X = 1 L = [1, 2]              changer(X, L)          # pass immutable and mutable print X, L                    L = [1, 2] changer(X, L[:])       # pass a copy print X, L