On Tue, 1 Aug 2017 at 02:32 Terry Reedy <tjre...@udel.edu> wrote: > On 7/31/2017 7:31 PM, t...@tomforb.es wrote: > > As part of the Python 3 cleanup in Django there are a fair few uses of > @functools.lru_cache on functions that take no arguments. > > This makes no sense to me. If the function is being called for > side-effects, then it should not be cached. If the function is being > called for a result, different for each call, calculated from a changing > environment, then it should not be cached. (Input from disk is an > example.) If the function returns a random number, or a non-constant > value from an oracle (such as a person), it should not be cached. If > the function returns a constant (possible calculated once), then the > constant should just be bound to a name (which is a form of caching) > rather than using the overkill of an lru cache. What possibility am I > missing? >
A function which is moderately expensive to run, that will always return the same result if run again in the same process, and which will not be needed in every session. Also particularly in the context of a large framework it may be neater & easier to expose a constant-like thing as a function to be called rather than directly bound to a name; if there are several implementations of a particular function, to be chosen based on configuration, and *some* of them are pure functions while others may change depending on the environment during a single run, maintaining the interface would trump simplicity for the simple case). I've not investigated the Django codebase, so I don't know if my guesses line up with it exactly, but it should be quite easy to construct a grep or sed script to scan the source to find out :) -- -- Matt Wheeler http://funkyh.at -- https://mail.python.org/mailman/listinfo/python-list