I was quite surprised by some behavior that I saw when assigning an argument to a class attribute in python. Perhaps someone can enlighten me, and help me stop it from happening.
Essentially, the changes I make to the class attribute inside the class methods are being replicated in the global variable that was passed to the class init as an argument.
Is there a built in way to stop this sort of behavior, as in a lot of cases it may break the data variable for other uses down the line.
Here is a basic version of the code
class BasicClass:
def __init__(self, data_raw):
self.data = data_raw
self.data['new_column'] = 1
# Now outside the class
data = pd.read_csv(...)
data.columns
Out[1]: ['orig_column']
obj = BasicClass(data)
data.columns
Out[2]: ['orig_column','new_column']