Save functions for re-using without re-execution

我的未来我决定 提交于 2020-01-24 01:39:10

问题


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


回答1:


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


来源:https://stackoverflow.com/questions/10230869/save-functions-for-re-using-without-re-execution

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!