保存函数以重新使用而不重新执行 [英] Save functions for re-using without re-execution

查看:113
本文介绍了保存函数以重新使用而不重新执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找到一些允许保存函数的东西,例如:一旦运行程序(包含某些函数),然后我们将函数的地址保存在内存中,然后我们可以重新使用这些函数而不执行任何更多。你能给我一些想法吗(通常或特别是Python,C / C ++,...)。我已经搜索了这个,但我没有得到:(我正在考虑某种内存管理(分配,内存自由,驻留内存......我猜)
例如:我有一个函数它的地址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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆