functools documentation - help with funny words

Veek M vek.m1234 at gmail.com
Sun Nov 9 04:06:50 EST 2014


https://docs.python.org/3.4/library/functools.html

1. "A key function is a callable that accepts one argument and returns 
another value indicating the position in the desired collation sequence."

x = ['x','z','q']; sort(key=str.upper)

My understanding is that, x, y, .. are passed to the key function which 
converts it to upper case and returns the value, which is then sorted.
So what does he mean by 'position in the desired..."? Position is 0, 1, 2..

upper is 'normalizing' the input and then sort gathers the values and sorts 
them.. so where is the 'key' function returning a position in the sorted-
list.. or whatever.. what does a key function do?

2. lru_cache

"Since a dictionary is used to cache results, the positional and keyword 
arguments to the function must be hashable."

basically says that the args must be immutable and not subject to change 
because he's using the args as part of a key to the result?

"To help measure the effectiveness of the cache and tune the maxsize 
parameter, the wrapped function is instrumented with a cache_info() function 
that returns a named tuple showing hits, misses, maxsize and currsize"

What does he mean by 'instrument'? Just a 'helper' function or is it nested 
in lru_cache or structured in some special way.

"An LRU (least recently used) cache works best when the most recent calls 
are the best predictors of upcoming calls (for example, the most popular 
articles on a news server tend to change each day)."

What? So if article1 changes.. how does that predict anything..? If article1 
is most popular, you want it in the cache, but it'll become most popular 
only after some time.. or is he saying that popular articles must be kicked 
out off the cache at the end of 24hrs?









More information about the Python-list mailing list