Short answer : Dictionary search first performs (cheap) equality of links ( x is y) when searching in a bucket, and only if this fails, a (more expensive) equality ( x == y) is checked .
Scenario
The function __hash__does not call __eq__ internally. If you create boband jim, such methods are not called.
bob 'tomorrow'. , , bob, . , , bob ( ).
jim. , jim, . . bob. (jim is bob), , . , , bob: 'tomorrow'.
, bob: we , . bob is bob, . (, ). 'tomorrow'.
, , () :
class Person(object):
def __init__(self, name, ssn, address):
self.name = name
self.ssn = ssn
self.address = address
def __hash__(self):
print('in hash')
return hash(self.ssn)
def __eq__(self, other):
print('in eq')
return False
False . :
>>> bob == bob
in eq
False
>>> bob is bob
True
bob ( , , : , ). , bob 'tomorrow', , bob:
>>> dmv_appointments = {}
>>> dmv_appointments[bob] = 'tomorrow'
in hash
>>> dmv_appointments[bob]
in hash
'tomorrow'