Python:是否有语法级别的支持从元组将参数解压缩到* anonymous *函数? [英] Python: Is there syntax-level support for unpacking, from tuples, the arguments to an *anonymous* function?

查看:103
本文介绍了Python:是否有语法级别的支持从元组将参数解压缩到* anonymous *函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们有以下内容:

args = (4,7,5)
def foo(a,b,c): return a*b%c

Python方便地允许元组解包:

Python conveniently allows tuple unpacking:

foo(4,7,5)             # returns 3
foo(*args)             # returns foo(4,7,5), i.e. 3

这样我们就不必这样做:

So that we don't have to do this:

foo(t[0], t[1], t[2])  # a repulsive, verbose, and error-prone synonym

现在假设我们有一个类似的三元组列表,并且想要一个 foo(t )每个元组 t 。有一种明显的方法:

Now suppose we had a list of similar 3-tuples and wanted a list of foo(t) for each tuple t. There is "one obvious way to do it":

list(map(lambda t: foo(*t), listoftuples))

但是现在假设 foo 只是一个一次性功能。我们不希望垃圾污染我们的命名空间。让我们在匿名的意义下扫一扫吧!:

But now suppose foo is just a throw-away function. We don't want rubbish polluting our namespace. Let's sweep it under the rug of anonymity!:

list(map(lambda t: (lambda a, b, c: a*b%c)(*t), listoftuples))

嗯,我们现在嵌套了lambda。当然可以了。但是我们冒着被策划人误解的风险,该策划人乐于构造神秘的咒语,其唯一目的是将那些自以为是的人堆砌起来以审查我们的代码。

Well, we now have nested lambdas. Sure, we can parse that. But we run the risk of being mistaken for a schemer who delights in constructing cryptic spells for the sole purpose of stumping those presumptuous enough to review our code.

此外,这是对于这样一个简单的想法有点冗长。这似乎不是pythonic。 (在scala中,该内部lambda的等效项是(_ * _%_),假设上下文允许类型推断。如果此 pythonic,

Furthermore, this is kinda verbose for such a simple idea. This just does not seem pythonic. (In scala, the equivalent of that inner lambda is (_*_%_), assuming context allows type inference. If this was pythonic, wouldn't it be similarly concise?).

我们可以这样删除内部lambda:

We could remove that inner lambda this way:

list(map((lambda t: t[0] * t[1] % t[2]), listoftuples))

那是较短的,但令人反感。我发现使用幻数(而不是名称)来引用参数会导致错误。

That's shorter, but repulsive. I have found that using magic numbers (rather than names) to refer to parameters tends to cause errors.

如果看起来更像这样,那就太好了 p>

It would be great if it looked much more like this:

list(map((lambda a, b, c: a*b%c), listoftuples))

当然不是那样。这就像尝试调用 foo(args)。可以这么说,我们需要一个星号。这是一个可能的星号:

Of course, it couldn't be that. That's like trying to call foo(args). We need an asterisk, so to speak. Here's one possible asterisk:

def unpackInto(func): return lambda t: func(*t)

它使代码易于阅读:

list(map(unpackInto(lambda a, b, c: a*b%c), listoftuples))

但是我们必须一直从个人模块中导入它。

But we'd have to import that from a personal module all the time. That's not suitable for collaboration, and it's kind of annoying for one-time use.

TL; DR

我希望 unpackInto 成为语言的一部分。语法已经支持吗?在标准库中?

I want unpackInto to be part of the language. Is it already supported in syntax? In standard libraries?

推荐答案

starmap 它位于itertools软件包中

在我的示例中:

list(map(lambda t: foo(*t), listoftuples))

成为

list(starmap(foo, listoftuples))

看看为什么叫星图?并具有匿名功能:

See why it's called starmap? And with anonymous functions:

def unpackInto(func): return lambda t: func(*t)
list(map(unpackInto(lambda a, b, c: a*b%c), listoftuples))

成为

list(starmap(lambda a, b, c: a*b%c, listoftuples))

一个简单的四字母前缀 star 是星号 我一直在寻找。

A simple four-letter prefix, star, is the "asterisk" I was looking for.

因此,,标准库支持将元组中的参数解包为匿名函数,但仅适用于通过星图的地图。

So, yes, there is standard library support for unpacking parameters from tuples into anonymous functions but only for map, via starmap.

,不支持语法级,但是在python 2中提供了 语法级别支持。请参见Bakuriu的答案。最后一个将变为:

No, there is no syntax-level support, but there was syntax level support in python 2. See Bakuriu's answer. The last would become:

list(map(lambda (a, b, c): a*b%c, listoftuples))

哪个更简洁。不得不将带走了

Which is even more concise. It's almost a shame it had to be taken away.

再一次,lambda很少像列表理解那样简洁或可读:

Then again, lambda is rarely as concise or readable as a list comprehension:

[a*b%c for a, b, c in listoftuples]

这篇关于Python:是否有语法级别的支持从元组将参数解压缩到* anonymous *函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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