3. Hits and misses¶
This is a summary of what constitutes a “hit” or a “miss”, for which the information is scattered around in the API Reference.
a hit refers to the successful retrieval of a value associated with a given key from the
LRUDictobject itself, and
a miss refers to one such attempt that fails.
An “insert”-like method typically neither hits nor misses, and neither does deleting a key-value pair (no matter whether the key exists). For instance, the index-assignment statement
L["watermelon"] = "green"
neither hits nor misses no matter what. The
does not affect hit/miss stats either, even if it may fail and raise
Somewhat special is the
setdefault() method that may either
“retrieve” from the
LRUDict – in the case that the given key is “in”;
or “insert” in it – in the case that there is not yet a key in it. The former
counts as a hit, because the value associated with the given key is indeed
obtained from the
LRUDict. However, the latter is not a miss, because
it is an insert-only operation: no attempt has been made to retrieve the value
associated with the given key.
Merely testing for membership using the
not in operator
neither hits nor misses.
colour = L.get("apple", "red")
incurs a miss if the key
not in L, and it will not
KeyError. The substitute value
"red", though assigned to
colour, is missed by
The tallying of hits/misses is orthogonal to other side effects. For example,
pop() is a “destructive” way to retrieve a value. It can hit
or miss, and in the case of a hit the item is removed from the
LRUDict. The fact that a hit is scored does not imply that the item is
It is safe to say that a key, if hit and extant, will always be promoted to the most-recently used one, if it is not so already. The converse is not true: assigning a value to an existing key will promote the key to the most recent, but it is not considered a hit.