In Python 3, given public & private functions, something like this:
def my_public_function(a, b, c) -> int:
rv = _my_private_function(a, b, c)
return rv
def _my_private_function(a, b, c) -> int:
return a + b + c
If I want to optimize this using functools.lru_cache
, am I better to do that on the public function or the private one?
My instinct is the public function so that it's cached as early as possible and returned without touching anything further.
from functools import lru_cache
@lru_cache()
def my_public_function...
Is there any design pattern or other design principle which would suggest using @lru_cache()
on _my_private_function
instead?
You save time (a function call and 2 adds) by handling this in the public function. In python there isn't really a notion of "private" functions. The convention starting a name with _ to tell others to stay away but there is nothing language specific to it. So it has no affect on any calling function, including lru_cache.
There is no general-purpose answer to this, because there basically aren't any cases where the choice of where to put the cache has no effect. If there were such a case, put it on the public function to skip the extra function call and whatever other setup work the public function has to do. But that's just never going to come up. Instead, it's going to be something like:
And so on.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.