[英]Python analog for Groovy's it?
对于简单的Clojures,Groovy具有很好的语法,从而无需直接命名单个参数。 默认情况下将被命名它 。
def closure = { print it }
closure( "hi there" ) //prints "hi there"
什么是效仿 magikal在Python参数的最佳方式?
我想要的例子:
>>> it = It()
>>> print map(it + 5, [1, 2, 3])
[6, 7, 8]
到目前为止,我已经找到了问题的确切答案。 这是fn.py库,它实现_ magikal变量和一些可用的函数式编程概念。
使用示例:
>>> from fn import _
>>> list(map(_ * 2, range(5)))
[0,2,4,6,8]
我的第一个想法是使用产生可弯曲函数的魔术方法来做一些魔术对象。
class It :
def __add__(self, another_one) :
return lambda x: x + another_one
it = It()
print map(it + 5, [1, 2, 3])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.