Sobes.tech
Junior — Senior

How does a dictionary handle user-defined class objects as keys

livecode

Task condition

It is necessary to understand whether an assert check will occur when an instance of its own class C is used as a key in the dictionary my_dict. The instance is created twice: when assigning my_dict[C(5)] = 10 and when checking assert my_dict[C(5)] == 10.

class C:
    def __init__(self, n) -> None:
        self._n = n

my_dict[C(5)] = 10
assert my_dict[C(5)] == 10