Python: functools


  1. lru_cache
    import functools, time
    
    
    @functools.lru_cache(maxsize=128, typed=False)
    def b(x, y, z=3):
        time.sleep(2)
        return x + y
    
    
    print(b(5, 6))
    print('~' * 60)
    print(b(5, 6))  # immediate
    
    print(b(5.0, 6))  # immediate  11
    import functools, time
    
    
    @functools.lru_cache(maxsize=128, typed=True)
    def b(x, y, z=3):
        time.sleep(2)
        return x + y
    
    
    print(b(5, 6))
    print('~' * 60)
    print(b(5, 6))  # immediate
    
    print(b(5.0, 6))  # not immediate 11.0
    import functools, time
    
    
    @functools.lru_cache(maxsize=128, typed=True)
    def b(x, y=6, z=3):
        time.sleep(2)
        return x + y
    
    
    print(b(5, 6))
    print('~' * 60)
    print(b(5))  # not immediate
    print('~' * 60)
    print(b(5, y=6))  # not immedate