I need to protect a class variable. But what to do if the class supports save and load options?
import numpy as np
import pickle
class data(object):
def __init__(self):
self.__a = range(100)
@property
def a(self):
return self.__a
def save(self, path):
pickle.dump(self,open(path, 'wb'), protocol=2)
def load(self, path):
obj = pickle.load(open(path, 'wb'))
self.__a = obj.a
This is simple but __a
attribute is no more protected because calling instance.a
returns the exact instance.__a
list and it can changed from the outside which is dangerous in my case.
is there any way around this?