How would one copy.deepcopy()
all but one part of an object in python?
I have an object which is basically a wrapper with some settings and some extra bits of metadata around a potentially huge pandas DataFrame. The DataFrame can contain arbitrarily huge amounts of data. I want to make a copy the object that consists of a shallow copy of the dataframe and a deepcopy() of the settings and metadata (both of which which can be mutable objects).
I don't know at run time if all of the settings and metadata exist when the copy is needed. There is also the possibility to people may set additional parts of the object using my_object.extra_setting
. This means that I can not just explicitly deepcopy all the parts of the object except the large dataframe.
The class is:
class my_class(object):
def __init__(self, lots_of_data, small_amount_of_data, setting_1, setting_2, setting_3):
self.lots_of_data = lots_of_data
self.small_amount_of_data = small_amount_of_data
self.setting_1 = setting_1
self.setting_2 = setting_2
def set_setting_3(self, setting_3):
self.setting_3 = setting_3
def set_more_metadata(metadata):
self.more_metadata = metadata
And in pseudocode the copy method is:
def __deepcopy__(self):
copy_of_object = copy.deepcopy(self[all but object_in.lots_of_data])
copy_of_object.lots_of_data = self.lots_of_data
return copy_of_object