Python "with" Keyword in Lambda Functions

lambda, python

Solution

`lambda_form ::= "lambda" [parameter_list]: expression`

You can't, `with` is a statement, and `lambda` only returns expressions.

Problem

How is Python's `with` keyword expressed in a lambda function? Consider the following: ``` def cat (filename): with open(filename, 'r') as f: return f.read() ``` A failed attempt at a lambda implementation: ``` cat = lambda filename: with open(filename, 'r') as f: return f.read() ```

Original source