Sobes.tech
Junior — Senior

Can an instance of the KeyHolder class be used as a dictionary key?

livecode

Task condition

The task considers the class KeyHolder. You need to determine whether its object can serve as a key in the standard Python dictionary. If the current implementation is not suitable, describe what changes should be made to the class so that its instances function correctly as keys. Here's an example of the initial code:

class KeyHolder:
    def __init__(self, key):
        self.key = key

    def __hash__(self):
        return 0

Note that for use as a key, the methods __hash__ and __eq__ are important. Suggest the necessary improvements.