title: Why are assignments not allowed in Python's `lambda` expressions?
body: <p>This is not a duplicate of <a href="https://stackoverflow.com/questions/6282042/assignment-inside-lambda-expression-in-python">Assignment inside lambda expression in Python</a>, i.e., I'm <em>not</em> asking how to trick Python into assigning in a <code>lambda</code> expression.</p>
  
  <p>I have some λ-calculus background.  Considering the following code, it
  looks like Python is quite willing to perform side-effects in <code>lambda</code>
  expressions:</p>
  
  <pre><code>#!/usr/bin/python
  
  def applyTo42(f):
      return f(42)
  
  def double(x):
      return x * 2
  
  class ContainsVal:
      def __init__(self, v):
          self.v = v
  
      def store(self, v):
          self.v = v
  
  def main():
  
      print('== functional, no side effects')
  
      print('-- print the double of 42')
      print(applyTo42(double))
  
      print('-- print 1000 more than 42')
      print(applyTo42(lambda x: x + 1000))
  
      print('-- print c\'s value instead of 42')
      c = ContainsVal(23)
      print(applyTo42(lambda x: c.v))
  
  
      print('== not functional, side effects')
  
      print('-- perform IO on 42')
      applyTo42(lambda x: print(x))
  
      print('-- set c\'s value to 42')
      print(c.v)
      applyTo42(lambda x: c.store(x))
      print(c.v)
  
      #print('== illegal, but why?')
      #print(applyTo42(lambda x: c.v = 99))
  
  if __name__ == '__main__':
      main()
  </code></pre>
  
  <p>But if I uncomment the lines</p>
  
  <pre><code>    print('== illegal, but why?')
      print(applyTo42(lambda x: c.v = 99))
  </code></pre>
  
  <p>I'll get</p>
  
  <pre><code>SyntaxError: lambda cannot contain assignment
  </code></pre>
  
  <p>Why not?  <strong>What is the deeper reason behind this?</strong></p>
  
  <ul>
  <li><p>As the code demonstrates, it cannot be about “purity” in a
  functional sense.</p></li>
  <li><p>The only explanation I can imagine is that assignemts do not
  return anything, not even <code>None</code>.  But that sounds lame and would
  be easy to fix (one way: make lambda expressions return <code>None</code> if
  body is a statement).</p></li>
  </ul>
  
  <p>Not an answer:</p>
  
  <ul>
  <li><p>Because it's defined that way (I want to know why it's defined that way).</p></li>
  <li><p>Because it's in the grammar (see above).</p></li>
  <li><p>Use <code>def</code> if you need statements (I did not ask for how to get
  statements into a function).</p></li>
  </ul>
  
  <p>“This would change syntax / the language / semantics” would be ok as an answer if you can come up with an example of such a change, and why it would be bad.</p>