python lambda raises variable not defined error with multiple arguments
<p>When trying to write a one line Fibonacci sequence that I understand, I'm having an issue with <code>fib = lambda a, b: b, a + b</code> as <code>"'b' is not defined"</code></p>

<p>However, when I do <code>sum = a, b, c: a + b + c</code> I get no errors.  <code>sum(1, 2, 3)</code> runs perfectly and returns <code>6</code>.</p>

<p>I've researched global variables and found that if I set a and b to Null before starting, it doesn't give me an error, but is there a way not to do this?</p>

<p>You need to put parentheses around the lambda body:</p>

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

<p>Otherwise Python thinks it is this:</p>

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

<p>Incidentally, there's no real purpose in using <code>lambda</code> if you're just going to assign the function to a name.</p>

python python-3.x lambda