12/40
Part 2 of 7 · KeysLearn and do

A key that changes after it goes in can't be found

A key's hash has to stay the same for as long as the key sits in the table. Here is what happens when it doesn't.

class Box:
    def __init__(self, label):
        self.label = label

    def __eq__(self, other):
        return self.label == other.label

    def __hash__(self):
        return hash(self.label)


box = Box("a")
boxes = {box}
box.label = "z"
print(box in boxes, Box("a") in boxes, Box("z") in boxes, len(boxes))
Predict

A Box with label "a" goes in a set, and then its label changes to "z". Is Box("z") in the set?

Pick one to see what it prints.
Pick an answer above to see the rest