title: Applying `functools.lru_cache` to lambda
body: <p>So I made a recursive lambda in Python for the Fibonacci sequence. I used recursion because it was easiest to implement with lambda.</p>
  
  <pre><code>fib = lambda n: fib(n - 1) + fib(n - 2) if n &gt; 1 else 1
  </code></pre>
  
  <p>Because using recursion, the same Fibonacci values were calculated many times, I thought using a cache decorator would help, and I knew that <code>functools.lru_cache</code> was an easy option. </p>
  
  <p>I know you can't apply the decorator to the function using <code>@functools.lru_cache</code> to a lambda like a normal function, but when I tried this:</p>
  
  <pre><code>fib = functools.lru_cache((lambda n: fib(n - 1) + fib(n - 2) if n &gt; 1 else 1), maxsize = None)
  </code></pre>
  
  <p>I got an error saying that <code>functools.lru_cache</code> wouldn't accept a function object as an argument.</p>
  
  <pre><code>&gt;&gt;&gt; fib = functools.lru_cache((lambda n: fib(n - 1) + fib(n - 2) if n &gt; 1 else 1), maxsize = None)
  Traceback (most recent call last):
    File "&lt;pyshell#2&gt;", line 1, in &lt;module&gt;
      fib = functools.lru_cache((lambda n: fib(n - 1) + fib(n - 2) if n &gt; 1 else 1), maxsize = None)
  TypeError: lru_cache() got multiple values for argument 'maxsize'
  </code></pre>
  
  <p>I checked the docs, and it looks like <code>functools.lru_cache</code> only accepts a <code>maxsize</code> and a <code>typed</code> argument, which default to <code>128</code> and <code>False</code> respectively.</p>
  
  <p>Is there a cleverer way to assign the decorator to the function rather than just defining the function without lambda and then applying the decorator?</p>