Created
August 5, 2020 13:15
-
-
Save raeq/de0b5a823b68fb85557fbdcd0b5cd4d7 to your computer and use it in GitHub Desktop.
Dictionary deepcopy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import copy | |
original = {} | |
original["object1"] = dict({"a": {"b": [1, 2, 3]}}) | |
dict_shallow = original.copy() | |
dict_deep = copy.deepcopy(original) | |
# change the mutable object in original and dict_shallow | |
original["object1"]["a"]["b"] = [3, 4, 5] | |
assert id(original["object1"]) == id(dict_shallow["object1"]) | |
assert id(original["object1"]) != id(dict_deep["object1"]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment