Consider the following code:
class DummyClass:
def __init__(self, name):
self.name = name
obj_1 = DummyClass("name_1")
obj_2 = DummyClass("name_2")
my_dict = {
obj_1: "value_1",
obj_2: "value_2"
}
print(my_dict)
Output
{<__main__.DummyClass object at 0x7f8f6493a0b8>: 'value_1', <__main__.DummyClass object at 0x7f8f649869e8>: 'value_2'}
This code created a dictionary my_dict
that has instances of the DummyClass
as keys.
I would like to know if there is a magic function (e.g. __something__
) that I can override in the DummyClass
so that the code above would produce the following output:
{'name_1': 'value_1', 'name_2': 'value_2'}
where name_1
and name_2
are both strings, the name
properties of the DummyClass
instances.
I don't want to change visualization or the print, I want to actually change the key in memory.