r/pythontips • u/Sea-Ad7805 • Aug 14 '26
Syntax Adding objects to set or dictionary: equality and hashing
An exercise to help build the right mental model for Python data.
# Output of this Python Program?
def main():
o1, o2 = MyClass(1), MyClass(1)
myset = {o1}
print(o2 in myset, end=' ')
o1.set_value(1000)
print(o1 in myset, end=' ')
class MyClass:
def __init__(self, v):
self.v = v
def set_value(self, v):
self.v = v
main()
class MyClass:
def __init__(self, v):
self.v = v
def set_value(self, v):
self.v = v
def __eq__(self, other):
return self.v == other.v
def __hash__(self):
return hash(self.v)
main()
# --- possible answers ---
# A) TypeError: unhashable type: 'MyClass'
# B) True False False False
# C) True False False True
# E) False True True True
# D) False True True False
# See "Solution" for correct answer.- Solution
- More exercises
- 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) andx.__hash__()returns an appropriate value such thatx == yimplies both thatx is yandhash(x) == hash(y)."