Python模拟Groovy是什么?

2024-04-24 00:41:16 发布

您现在位置:Python中文网/ 问答频道 /正文

Groovy对于简单的clojure有很好的语法,这样就不需要显式地命名 单一参数。默认情况下,它将被命名为It。你知道吗

def closure = { print it }
closure( "hi there" ) //prints "hi there"

在Python中模拟itmagikal参数的最佳方法是什么?你知道吗

我想要的例子:

>>> it = It()
>>> print map(it + 5, [1, 2, 3])
[6, 7, 8]

Tags: 参数def语法情况ithiprints命名
3条回答

只需给lambda或函数一个显式第一个参数:

lambda it: ' {}'.format(it)

这符合Zen of Python

Explicit is better than implicit

到目前为止,我已经找到了我问题的确切答案。这是图书馆fn.py公司它实现了magikal变量和一些有用的函数式编程概念。你知道吗

使用示例:

>>> from fn import _
>>> list(map(_ * 2, range(5)))
[0,2,4,6,8]

我的第一个想法是用magik方法生成curryied函数来实现magikalit对象。你知道吗

class It :
    def __add__(self, another_one) :
         return lambda x: x + another_one



it = It()
print map(it + 5, [1, 2, 3])

相关问题 更多 >