Python中没有多行Lambda:为什么不呢? [英] No Multiline Lambda in Python: Why not?

查看:124
本文介绍了Python中没有多行Lambda:为什么不呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我听说它不能在Python中添加多行lambda,因为它们会在语法上与Python中的其他语法结构发生冲突.我今天在公交车上正在考虑这一点,意识到我无法想到与多行lambda冲突的单个Python构造.鉴于我对这门语言非常了解,这让我感到惊讶.

I've heard it said that multiline lambdas can't be added in Python because they would clash syntactically with the other syntax constructs in Python. I was thinking about this on the bus today and realized I couldn't think of a single Python construct that multiline lambdas clash with. Given that I know the language pretty well, this surprised me.

现在,我确定Guido有一个理由不在语言中包含多行lambda,而是出于好奇:在什么情况下包含多行lambda会变得模棱两可?我所听到的是真的,还是Python不允许多行lambda的其他原因?

Now, I'm sure Guido had a reason for not including multiline lambdas in the language, but out of curiosity: what's a situation where including a multiline lambda would be ambiguous? Is what I've heard true, or is there some other reason that Python doesn't allow multiline lambdas?

推荐答案

查看以下内容:

map(multilambda x:
      y=x+1
      return y
   , [1,2,3])

这是返回(y, [1,2,3])的lambda吗(因此映射仅获得一个参数,从而导致错误)?还是返回y?还是语法错误,因为新行上的逗号放置不正确? Python怎么知道您想要什么?

Is this a lambda returning (y, [1,2,3]) (thus map only gets one parameter, resulting in an error)? Or does it return y? Or is it a syntax error, because the comma on the new line is misplaced? How would Python know what you want?

在括号内,缩进对于python并不重要,因此您不能明确地使用多行.

Within the parens, indentation doesn't matter to python, so you can't unambiguously work with multilines.

这只是一个简单的例子,可能还有更多示例.

This is just a simple one, there's probably more examples.

这篇关于Python中没有多行Lambda:为什么不呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆