Python lazy evaluator

后端 未结 7 2256
心在旅途
心在旅途 2020-12-19 05:14

Is there a Pythonic way to encapsulate a lazy function call, whereby on first use of the function f(), it calls a previously bound function g(Z) an

相关标签:
7条回答
  • 2020-12-19 06:03

    You can employ a cache decorator, let see an example

    from functools import wraps
    
    class FuncCache(object):
        def __init__(self):
            self.cache = {}
    
        def __call__(self, func):
            @wraps(func)
            def callee(*args, **kwargs):
                key = (args, str(kwargs))
                # see is there already result in cache
                if key in self.cache:
                    result = self.cache.get(key)
                else:
                    result = func(*args, **kwargs)
                    self.cache[key] = result
                return result
            return callee
    

    With the cache decorator, here you can write

    my_cache = FuncCache()
    
    @my_cache
    def foo(n):
        """Expensive calculation
    
        """
        sum = 0
        for i in xrange(n):
            sum += i
        print 'called foo with result', sum
        return sum
    
    print foo(10000)
    print foo(10000)
    print foo(1234)
    

    As you can see from the output

    called foo with result 49995000
    49995000
    49995000
    

    The foo will be called only once. You don't have to change any line of your function foo. That's the power of decorators.

    0 讨论(0)
提交回复
热议问题