Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Python 的with关键字在 lambda 函数中是如何表达的?考虑以下:
with
def cat (filename): with open(filename, 'r') as f: return f.read()
lambda 实现尝试失败:
cat = lambda filename: with open(filename, 'r') as f: return f.read()
lambda_form ::= "lambda" [parameter_list]: expression
你不能,with是一个语句,并且lambda只返回表达式。
lambda
以防万一有人在寻找技巧:
lambda filename: [(f.read(), f.close()) for f in [open(filename)]][0][0]