On Mon, 27 Apr 2020 10:11:01 +1000 Steven D'Aprano <steve@pearwood.info> wrote:
On Sun, Apr 26, 2020 at 06:43:06PM +0200, Alex Hall wrote:
It's not clear to me why people prefer an extra function which would be exactly equivalent to lru_cache in the expected use case (i.e. decorating a function without arguments). It seems like a good way to cause confusion, especially for beginners. Based on the Zen, there should be one obvious way to do it.
Indeed, and if you want to guarantee that a function is executed exactly *once*, then a decorator called *once* is that Obvious Way.
How many beginners do you know who even know what a LRU cache is?
The same number of beginners who know to look in functools for a decorator called once? The same number of beginners who create multithreaded programs without understanding what they're getting into, or what they've gotten themselves into once they lose the race condition? The same number of beginners who think, "hmm, I wonder whether I have to keep calling this expensive, zero arity function over and over, or there's some standard library function to prevent that?" Yes, I say those things with at least part of my tongue in part of my cheek. Depending on a definition of "beginner," IMO beginners don't run into this use case. Also, a decorator called once is Obvious, *once* (pun intended) you know it's there. Dan