Python: Use lru_cache on lambda function, or other ways to create cache for lambda function
<p>Is there anyway to make it work?</p>

<pre><code>func=i_want_it_to_cache_everything(lambda a,b:a+b)
</code></pre>

<p>And it <strong>has to be done in one line</strong>...</p>

<p>Update2:</p>

<p>I figured out the solution (thanks to everyone who replied!). 
But... There is an interesting phenomenon: caching slows down program?</p>

<pre><code>import functools,datetime,timeit
@functools.lru_cache(maxsize=50000)
def euclidean_distance3(p1,p2):
    return (p1[0]-p2[0])**2+(p1[1]-p2[1])**2+(p1[2]-p2[2])**2+(p1[3]-p2[3])**2
euclidean_distance=(functools.lru_cache(maxsize=50000)(lambda p1,p2: (p1[0]-p2[0])**2+(p1[1]-p2[1])**2+(p1[2]-p2[2])**2+(p1[3]-p2[3])**2))
euclidean_distance2=lambda p1,p2: (p1[0]-p2[0])**2+(p1[1]-p2[1])**2+(p1[2]-p2[2])**2+(p1[3]-p2[3])**2
print(datetime.datetime.now())
def test1():
    for z in range(50):
        for i in range(200):
            for j in range(200):
                euclidean_distance((i,i,i,i),(j,j,j,j));
def test2():
    for z in range(50):
        for i in range(200):
            for j in range(200):
                euclidean_distance2((i,i,i,i),(j,j,j,j));
def test3():
    for z in range(50):
        for i in range(200):
            for j in range(200):
                euclidean_distance3((i,i,i,i),(j,j,j,j));
t1=timeit.Timer(test1)
print(t1.timeit(1))
t2=timeit.Timer(test2)
print(t2.timeit(1))
t3=timeit.Timer(test3)
print(t3.timeit(1))

print(euclidean_distance.cache_info())
print(euclidean_distance3.cache_info())
</code></pre>

<p>output:</p>

<pre><code>9.989034592910151
4.936129879313011
10.528836308312947
CacheInfo(hits=1960000, misses=40000, maxsize=50000, currsize=40000)
CacheInfo(hits=1960000, misses=40000, maxsize=50000, currsize=40000)
</code></pre>
