Skip to content Skip to sidebar Skip to footer

Preventing Reference Re-use During Deepcopy

Consider the following example: from copy import deepcopy item = [0] orig = [item, item] copy = deepcopy(orig) orig[0][0] = 1 print(f'{orig=} {copy=}') copy[0][0] = 2 print(f'{o

Solution 1:

If your whole point is to copy data that could come from JSON, i.e. list, dict, string, numbers, bool, then you can trivially implement your own function:

def copy_jsonlike(data):
    if isinstance(data, list):
        return [copy_jsonlike(x) for x indata]
    elif isinstance(data, dict):
        return {k: copy_jsonlike(v) for k,v indata.items()}
    else:
        returndata

It has the added bonus of probably being faster than copy.deepcopy

Or, your original solution, json.loads(json.dumps(data)) isn't a bad idea either.

Solution 2:

Huh, seems like this was easier to do than I thought, but I'm 90% sure it's evil. If someone posts a better answer or explains why this is totally awful, I'll remove it.

Implement a dict that only pretends to set a value. Then the example returns separate copies of the same reference.

classNoMemo(dict):def__setitem__(self, key, value):
        return value
...
copy = deepcopy(orig, memo=NoMemo())
...

Prints:

orig=[[1], [1]] copy=[[0], [0]]
orig=[[1], [1]] copy=[[2], [0]]

Post a Comment for "Preventing Reference Re-use During Deepcopy"