保存函数以重新使用而不重新执行 [英] Save functions for re-using without re-execution
问题描述
例如:我有一个函数它的地址at(在程序运行时产生)我如何重用它们?!
在此先感谢
<好的,在Python中,函数是对象,所以你可以传递它们,分配它们,并从任何你用来别名的标签中调用它们,你也可以得到id(内存位置/ guid等价)。你的意思是记忆/延迟加载数据,在SO和Google上有很多资源可以用来做这类事情。它们通常是这样的:
class Foo(object):
@staticmethod
def get_val():
try:
返回Foo .__ val
,但AttributeError :
#在此处运行一次逻辑,分配给Foo .__ val
返回Foo._ _val
I tried to find some kind of things that allow to "save functions" such as: Once running the program (included some functions) and we will save functions's address in memory after that, we can re-use these functions without execution any more. Could you give me some ideas (in general or particular in Python, C/C++,...). I have googled this but I didn't get it :(. I was thinking of some kind of memory management (allocation, freedom of memory, resident memory...I guess) For example: I have a function with its address "at " (that is generated when program runs) How can I reuse them?! Thanks in advance
Well, in Python, functions are objects, so you can pass them around, assign them, and call them from any label you've used to alias them. You can also get the id (memory location/guid equivalent). If what you mean is memoization/lazy loading of data, there are a number of resources available on SO and through Google for doing that sort of thing. They generally look like:
class Foo(object):
@staticmethod
def get_val():
try:
return Foo.__val
except AttributeError:
#do run-once logic here, assign to Foo.__val
return Foo.__val
这篇关于保存函数以重新使用而不重新执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!