python 在其他函数中使用一个函数的输出而不调用所有其他函数
python use output from one function in other functions without calling all the other functions
我有一个核心函数,我从脚本中的许多其他函数调用它。问题是我不希望每个函数在调用核心函数时都运行它。有没有一种方法可以存储核心函数的输出,以便在第二次、第三次等调用它时,它不是 运行?
例如
def core_func(a,b,c):
do something....
return x,y,z
def func2(a,b,c):
x,y,z = core_func(a,b,c)
do something with x,y,z
def func3(a,b,c):
x,y,z = core_func(a,b,c)
do something with x,y,z
等..
func3 会在 func2 调用后再次调用 core_func。如何防止这种情况但同时使用 core_func 输出?一个可能的解决方案可能是 return func2 的输出并在 func3 中使用(但这会变得有点难看)。
谢谢
variable = core_func(arguments)
func2(variable)
func3(variable)
将函数的结果存储在一个变量中!
您可以使用memoize
Caches a function's return value each time it is called.
因此,每次使用相同的参数调用函数时,您将得到 return 的值,但没有计算时间
即:
如果你正在使用 Python2 你需要实现它,你可以看看它是如何在上面的 link 上实现的,然后将它应用到你的函数中:
class memoized(object):
'''Decorator. Caches a function's return value each time it is called.
If called later with the same arguments, the cached value is returned
(not reevaluated).
'''
def __init__(self, func):
self.func = func
self.cache = {}
def __call__(self, *args):
if not isinstance(args, collections.Hashable):
# uncacheable. a list, for instance.
# better to not cache than blow up.
return self.func(*args)
if args in self.cache:
return self.cache[args]
else:
value = self.func(*args)
self.cache[args] = value
return value
def __repr__(self):
'''Return the function's docstring.'''
return self.func.__doc__
def __get__(self, obj, objtype):
'''Support instance methods.'''
return functools.partial(self.__call__, obj)
@memoized
def core_func(a, b, c):
do something....
return x,y,z
如果您使用的是 Python3,您可以通过 lru_cache decorator
免费获得
Decorator to wrap a function with a memoizing callable that saves up
to the maxsize most recent calls. It can save time when an expensive
or I/O bound function is periodically called with the same arguments.
from functools import lru_cache
@lru_cache(maxsize=32)
def core_func(a, b, c):
do something....
return x,y,z
我有一个核心函数,我从脚本中的许多其他函数调用它。问题是我不希望每个函数在调用核心函数时都运行它。有没有一种方法可以存储核心函数的输出,以便在第二次、第三次等调用它时,它不是 运行?
例如
def core_func(a,b,c):
do something....
return x,y,z
def func2(a,b,c):
x,y,z = core_func(a,b,c)
do something with x,y,z
def func3(a,b,c):
x,y,z = core_func(a,b,c)
do something with x,y,z
等..
func3 会在 func2 调用后再次调用 core_func。如何防止这种情况但同时使用 core_func 输出?一个可能的解决方案可能是 return func2 的输出并在 func3 中使用(但这会变得有点难看)。
谢谢
variable = core_func(arguments)
func2(variable)
func3(variable)
将函数的结果存储在一个变量中!
您可以使用memoize
Caches a function's return value each time it is called.
因此,每次使用相同的参数调用函数时,您将得到 return 的值,但没有计算时间
即:
如果你正在使用 Python2 你需要实现它,你可以看看它是如何在上面的 link 上实现的,然后将它应用到你的函数中:
class memoized(object):
'''Decorator. Caches a function's return value each time it is called.
If called later with the same arguments, the cached value is returned
(not reevaluated).
'''
def __init__(self, func):
self.func = func
self.cache = {}
def __call__(self, *args):
if not isinstance(args, collections.Hashable):
# uncacheable. a list, for instance.
# better to not cache than blow up.
return self.func(*args)
if args in self.cache:
return self.cache[args]
else:
value = self.func(*args)
self.cache[args] = value
return value
def __repr__(self):
'''Return the function's docstring.'''
return self.func.__doc__
def __get__(self, obj, objtype):
'''Support instance methods.'''
return functools.partial(self.__call__, obj)
@memoized
def core_func(a, b, c):
do something....
return x,y,z
如果您使用的是 Python3,您可以通过 lru_cache decorator
免费获得Decorator to wrap a function with a memoizing callable that saves up to the maxsize most recent calls. It can save time when an expensive or I/O bound function is periodically called with the same arguments.
from functools import lru_cache
@lru_cache(maxsize=32)
def core_func(a, b, c):
do something....
return x,y,z