An exercise to help build the right mental model for Python data.
- Solution
- Explanation: “User-defined classes have __eq__() and __hash__() methods by default (inherited from the object class); with them, all objects compare unequal (except with themselves) and x.__hash__() returns an appropriate value such that x == y implies both that x is y and hash(x) == hash(y).”
#Python #memory_graph #Equality #Hashing
That’s a very complicated way to explain it though.
- You basically create two object instances
- You then put one of those instances in a variable
- No matter what you do to those instances, you don’t change the fact of #2.
It’s like putting your friend Sarah on a bike and then giving her Christina’s jacket. You haven’t changed the fact that Sarah is on the bike and Christina isn’t.
Sure, but a lot of people incorrectly think the
__eq__and__hash__are defined based on value not identity, as they are for many other types say float, str, or tuple. But for a class the default__eq__method isx is yinstead ofx == yand also__hash__is based on identity.Others assume that if you don’t define
__hash__for a class, that it doesn’t exists (like for list, set, or dict) so that a “TypeError: unhashable type: ‘Value’” exception is raised.I thought it was an interesting exercise to share, but maybe too simple for this audience, or people are just not aware of the basic steps that happen when adding and searching values in a set/dict. Try the same with type int:
v1 = 1001 v2 = 1002 myset = {v1} print(v1 in myset, end=' ') v2 = 1001 print(v2 in myset, end=' ') v1 = 1002 print(v1 in myset, end=' ')and you see a different output. To do the same with the
Valueclass add methods:def __eq__(self, other): return self.value == other.value def __hash__(self): return hash(self.value)I think you’re fundamentally misunderstanding the above.
In your first example, you change an attribute of an object. In your case you called it
.valuebut could just as well have been called.jacket.In the second case you replaced one immutable object with another. You didn’t change the “value” attribute of
1002to1001. You replaced the entire object,1002with a new one:1001.Yes indeed, the ‘int’ objects are stored and searched in the set based on their value, and the ‘Value’ objects are stored/searched based on their identity unless you define the above
__eq__and__hash__methods which cause them to be stored/searched based on value too. This is a Python design decision and that’s the point of this exercise. The fact that ‘int’ is an immutable type and we are replacing isn’t relevant. Try replacing/reassigning the ‘Value’ objects after added__eq__and__hash__, and you get the same result as for ‘int’. So the thing that really matters is how__eq__and__hash__are defined, and the default for a user-defined class is as stated in the “Explanation:” above.Maybe I should also show a class with
__eq__and__hash__defined based on value, but then it gets a bit long. I’ll have to rethink this exercises so that the point comes across better as it now seems to confuse a lot of people based on the down-votes. Thanks for feedback anyway.


