MarKre
MarKre

Reputation: 101

How to avoid unintentional changing a global variable when using it as a local variable (inside a function)?

Problem:

I am using a function that takes a (global) variable as an input, then performs operations on that variable (locally), and then returns that same variable. I do only want the variable to change locally, but instead my function is changing the global variable as well.

Code to reproduce:

data = {'A' : [1,2,3],
        'B' : [4,5,6],
        'C' : [7,8,9]}
df = pd.DataFrame(data)

def func(df):
    df['D'] = df['A'] * df['B'] / df['C']
    return df

func(df) # running function, without assigning it to original variable

print(df)

Returns:

Running the code shows that the original dataframe has been changed and a column was added.

   A  B  C   D
0  1  4  7  12
1  2  5  8  15
2  3  6  9  18

Expected behaviour:

My intention is to run the function without adding the column to the global variable, only add it locally within the function.

    A   B   C
0   1   4   7
1   2   5   8
2   3   6   9

Set-up:

Upvotes: 3

Views: 949

Answers (1)

Błotosmętek
Błotosmętek

Reputation: 12927

You can make a local copy:

def func(d):
    df = d.copy()
    df['D'] = df['A'] * df['B'] / df['C']
    return df

Upvotes: 2

Related Questions