Pass a copy of mutable data type to a function in Python

When we call a function, each of the parameters of the function is assigned to the object they were passed in. In essence, each parameter now becomes a new nickname to the objects that were given in.

If we pass in immutable arguments, then we have no way of modifying the arguments themselves. After all, that's what immutable means: "doesn't change".

On the other hand, mutable arguments can be changed. We can modify their internal contents. A prime example of a mutable object is a list: its elements can change (and so can its length).

The below example shows how to not change the value of the mutable object:

def func_0(other_num):
  other_num[0] += 1
  return other_num

start_num = [1]
finish_num = func_0(start_num[:]) # transfer a slice

# other method
finish_num = func_0(start_num.copy()) # transfer a copy

# Shallow copy method
import copy
finish_num = func_0(copy.copy(start_num)) # transfer a shallow copy