21/29
Part 5 of 6 · Snapshots, and a worked exampleLearn and do
Snapshots and restore
Some levels save the state and bring it back later. The saved copy is a snapshot. It asks three things of you: what to copy, what to keep, and what time means after the restore.
Saving a name saves nothing
import copy
tasks = {"backup": {"deadline": 130}}
# a second name for the same dict
saved = tasks
# a new outer dict holding the same inner dicts
shallow = dict(tasks)
# new all the way down
deep = copy.deepcopy(tasks)
tasks["backup"]["deadline"] = 999
tasks["report"] = {"deadline": 200}
print("saved ", saved)
print("shallow", shallow)
print("deep ", deep)
After the inner deadline becomes 999 and report is added, which copy sees which change?
Pick one to see what it prints.
Pick an answer above to see the rest