Assume I have a custom class CustomObject and I do not define a custom __hash__
or __eq__
function for it. Will there be any difference between the following two operations in terms of outputs in any conditions?
a = CustomObject(1)
b = CustomObject(1)
setA = set()
# option 1
setA.add(a)
print((b in setA))
# option 2
setA.add(id(a))
print((id(b) in setA))
According to What is the default __hash__ in python?, the default __hash__
function is bound to the id of the object, so I assume there is no difference between the above two options?
If I define custom __hash__
functions for CustomObject like in add object into python's set collection and determine by object's attribute, the above two options will be different, right?