Mission briefing:
#1 lrucache() - does not always cache when you'd expect
(given an f(a): f(5) != f(a=5), or at least these mean two different calculations into the cache, and could even yield different values)
#2 partial() - you can still set the 'frozen' parameters
To begin with:
#1 lrucache()
It's a great thing to have something like this in the core set of a programming language.
However, beware: it will not consider an argument passed in as a keyword argument equivalent to the same argument passed in at its respective position.
A StackOverflow question is already dedicated to the topic - it was a bit of a surprise to me that this is so.
The below code demonstrates what I was at first surprised to expect to happen once I took a look at the
lru_cache() implementation (for reasons irrelevant to the argument):
Next up:
#2 partial()
Now I must admit I am not a vigorous user of
partial(), but I would have expected it to resist attempts to override the already 'frozen' (word from the Python documentation) parameters.
"The partial() is used for partial function application which “freezes” some portion of a function’s arguments and/or keywords resulting in a new object with a simplified signature."
At least one risk here is that when part of a larger system, some code might change those values behind your back, breaking your assumptions.
I will try to remind myself to look at functions obtained via
partial() as if they were simply provided a default value for some of their arguments.