简体   繁体   English

保存功能以供重复使用而无需重新执行

[英]Save functions for re-using without re-execution

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++,...). 您能否给我一些想法(总体上或特定于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. 好吧,在Python中,函数是对象,因此您可以传递它们,对其进行分配并从您用来对其进行别名的任何标签调用它们。 You can also get the id (memory location/guid equivalent). 您还可以获取ID(等同于内存位置/ GUID)。 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. 如果您的意思是记忆化/延迟加载数据,那么SO上和通过Google都有大量资源可用于执行此类操作。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM