Pass immutable and mutable value into function : Function Parameters « Function « Python






Pass immutable and mutable value into 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


           
       








Related examples in the same category

1.Four different ways to pass parametersFour different ways to pass parameters
2.Passing value or passing addressPassing value or passing address
3.Pass string value into a functionPass string value into a function
4.Python functions are 'typeless'Python functions are 'typeless'