不多说,直接上代码。
from functools import partial
class F(partial):
def __ror__(self, other):
if isinstance(other, tuple):
return self(*other)
return self(other)
使用样例。
range(10) | F(filter, lambda x: x % 2) | F(sum)
更详细的前因后果介绍,可以看 https://aber.sh/articles/Python-Pipe/ ,实在是懒得复制到 V2EX 调样式了。有兴趣的就看看,没兴趣的就复制代码去用就完事。
小小的得瑟一下,这是最近几天写的最满意、最有用的代码。妙手偶得之~