Un dict ne cherche pas une clé — il la localise, en deux temps.A dict doesn't search for a key — it locates it, in two steps.
Le numéro précédent a posé le couple __eq__/__hash__ comme un contrat couplé, sans dire pourquoi le langage y tient tant. Voici la raison : un dict ou un set ne compare pas la clé cherchée à toutes les clés existantes, un par un — ce serait aussi lent qu'une liste. Il appelle hash(clé) pour obtenir un entier, s'en sert pour sauter directement à un compartiment interne, et ne compare avec == qu'à l'intérieur de ce compartiment, pour départager d'éventuelles collisions. La rapidité légendaire du dict repose entièrement sur ce raccourci — et le raccourci suppose une règle stricte.The previous issue set up the __eq__/__hash__ pair as a coupled contract, without saying why the language cares so much. Here's why: a dict or set doesn't compare a sought key against every existing key, one by one — that would be as slow as a list. It calls hash(key) to get an integer, uses it to jump straight to an internal bucket, and only compares with == inside that bucket, to break ties on collisions. The dict's legendary speed rests entirely on that shortcut — and the shortcut assumes a strict rule.
vus = {"alice", "bob", "carol"}
"bob" in vus
# 1. hash("bob") -> un entier -> choisit un compartiment
# 2. dans ce compartiment, Python compare "bob" == chaque clé qui s'y trouve
# 3. une égalité confirme -> True. Sans elle -> False, même compartiment ou pas.
# Un set ne parcourt PAS ses éléments un par un : il saute directement
# au compartiment désigné par le hash, puis ne compare qu'à l'intérieur.Avant de mettre un objet dans un set ou comme clé de dict, demande : son hash restera-t-il collé à son égalité, pour toute sa vie dans cette structure ? Si la réponse dépend d'un état qui peut changer, l'objet n'a rien à faire là.Before putting an object in a set or as a dict key, ask: will its hash stay glued to its equality, for its whole life inside that structure? If the answer depends on state that can change, the object has no business being there.
hash() ne prouve rien à lui seul : deux objets différents peuvent partager un hash (collision, normale et gérée). C'est == qui tranche. Le hash ne sert qu'à réduire la recherche à une poignée de candidats — sa seule obligation est de ne jamais séparer deux objets égaux dans deux compartiments différents.hash() alone proves nothing: two different objects can share a hash (a collision, normal and handled). == is what settles it. The hash only narrows the search to a handful of candidates — its one obligation is to never split two equal objects into different buckets.