Python Objects As Dictionary Keys

charlton_a

Charlton Asino

Posted on September 7, 2023

Python Objects As Dictionary Keys

When can it happen?
You have a custom class(object) and you want to store related metadata in memory . You decide to use Python dict and leverage the faster look ups O(1)  . 
In the case of a user object:

user class

Python dicts ,eq and the hash function
For the user object , tracking the number of clicks made the user is required for later computations.The object will be the key for our dict and the number of clicks will be the value .In this case the user will be instatiated more than once.



>>> u1=User(1,"Jill")
>>> clicks=dict()
>>> clicks[u1]=1
>>> clicks
{<User(1 Jill)>: 1}
>>> u2=User(2,"Lock")
>>> clicks[u2]=2
>>> clicks
{<User(1 Jill)>: 1, <User(2 Lock)>: 2}
>>> u3=User(1,"Jill")
>>> clicks[u3]=3
>>> clicks
{<User(1 Jill)>: 1, <User(2 Lock)>: 2, <User(1 Jill)>: 3}


Enter fullscreen mode Exit fullscreen mode

Both u1 andu3 are representations of the same user ,Jill with the same user_id 1 but when updating the value for u3a new key is created.

This happens because Python uses the object id to generate a hash which is used as a key in the dict.Both u1and u3 have different object ids hence will have different dictionary keys despite representing the same user.



>>> id(u1)
140030203095248
>>> id(u3)
140030203095440
>>> 


Enter fullscreen mode Exit fullscreen mode

 <User(1 Jill)>value needs to be updated for this case since it already exists in the dictionary .To a achieve this a __hash__ and__eq__ dunder methods should be implented for the user object where if the objects are equal they will have the same hash.

user class with eq and hash
Creating a dict with the user updated object gives the required output.



>>> u1=User(1,"Jill")
>>> clicks=dict()
>>> clicks[u1]=1
>>> clicks
{<User(1,Jill)>: 1}
>>> u2=User(2,"Lock")
>>> clicks[u2]=1
>>> clicks
{<User(1,Jill)>: 1, <User(2,Lock)>: 1}
>>> u3=User(1,"Jill")
>>> clicks[u3]=3
>>> clicks
{<User(1,Jill)>: 3, <User(2,Lock)>: 1}
>>>


Enter fullscreen mode Exit fullscreen mode

Upating theclicksdict with the object u3 which is represents the user Jill with user_id 1 ,updates the existing object in the dict u1 which is respresents the same user.

💖 💪 🙅 🚩
charlton_a
Charlton Asino

Posted on September 7, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Python Objects As Dictionary Keys
python Python Objects As Dictionary Keys

September 7, 2023