'Python function - Are variables copied or referenced? Best practice?

When I create a pandas dataframe

df = pd.DataFrame(...)

and give it to some function:

def func1(data):
   ax = data
   ax[:] = 1
   return ax 

Which I then call with:

func(df)
df

This returns the dataframe df with all values "1", so there is no local copy made of the variable of the function but rather a reference with ax=data which points at df.

Is there a best practice to make copy? Like calling func(df.deepcopy(True))? Or would you rather set ax = data.deepcopy(True) or this there even a better more efficient/intuitive way?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source