如何在不重复昂贵工作的情况下高效地多次访问函数的返回值?

huangapple go评论95阅读模式
英文:

How can I efficiently access a function's return value repeatedly without repeating expensive work?

问题

我想要多次访问Python函数的返回值,避免重复计算昂贵的部分。

示例:

  1. def expensive_function():
  2. print('进行昂贵的工作')
  3. return '昂贵工作的输出';
  4. def get_output_twice():
  5. output = expensive_function() # 这部分会执行昂贵的工作
  6. print(output) # 这部分不会执行昂贵的工作
  7. print(output) # 这部分不会执行昂贵的工作
  8. def get_output_again():
  9. output = expensive_function() # 这部分会再次执行昂贵的工作
  10. print(output) # 这部分不会执行昂贵的工作
  11. get_output_twice()
  12. print('\n')
  13. get_output_again()

这产生:

  1. 进行昂贵的工作
  2. 昂贵工作的输出
  3. 昂贵工作的输出
  4. 进行昂贵的工作
  5. 昂贵工作的输出

该示例演示了:

  1. 将返回值分配给变量允许多次访问,而无需再次调用函数,如get_output_twice()中所示。
  2. 将返回值分配给另一个函数中的变量会强制再次调用昂贵的函数。

是否可以通过多个其他函数的调用来访问函数的返回值,而不重复昂贵的工作并且不使用global

英文:

I would like to access a Python function's return value multiple times without repeating the computationally expensive parts.

Example:

  1. def expensive_function():
  2. print('do expensive work')
  3. return 'output of expensive work'
  4. def get_output_twice():
  5. output = expensive_function() # This does the expensive work
  6. print(output) # This does not do expensive work
  7. print(output) # This does not do expensive work
  8. def get_output_again():
  9. output = expensive_function() # This does the expensive work again
  10. print(output) # This does not do expensive work
  11. get_output_twice()
  12. print('\n')
  13. get_output_again()

This produces:

  1. do expensive work
  2. output of expensive work
  3. output of expensive work
  4. do expensive work
  5. output of expensive work

This example demonstrates:

  1. Assigning the return value to a variable allows that variable to be accessed multiple times without calling the function again, as demonstrated in get_output_twice().
  2. Assigning the return value to a variable in another function forces the expensive function to be called again.

Is it possible to access a function's return value via calls from multiple other functions without repeating expensive work and without the use of global?

答案1

得分: 3

可以使用函数缓存来实现这个目标。函数缓存是一种技术,它会将昂贵函数调用的结果存储在内存中,以便在以后再次使用相同输入调用该函数时进行重用。

  1. from functools import lru_cache
  2. @lru_cache(maxsize=None)
  3. def expensive_function():
  4. print('do expensive work')
  5. return 'output of expensive work'

maxsize=None 表示缓存可以无限增长,缓存函数的所有可能输入-输出对)。

英文:

Yes, you can use function caching to achieve this. Function caching is a technique where the result of an expensive function call is stored in memory and reused later when the function is called again with the same input.

  1. from functools import lru_cache
  2. @lru_cache(maxsize=None)
  3. def expensive_function():
  4. print('do expensive work')
  5. return 'output of expensive work'

(maxsize=None indicates that the cache can grow indefinitely and cache all possible input-output pairs of the function).

huangapple
  • 本文由 发表于 2023年4月17日 07:29:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76030807.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定