簡體   English   中英

用於Groovy的Python模擬是嗎?

[英]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]

您只需給您的lambda或函數一個明確的第一個參數:

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

這符合PythonZen

顯式勝於隱式

到目前為止,我已經找到了問題的確切答案。 這是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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM