废话少说,下面我们看用python怎么实现unix管道风格的函数调用.
#coding=utf-8
class Pipe:
def __init__(self, func):
self.func = func
def __ror__(self, other):
return self.func(other)
@Pipe
def add(args):
return sum(args)
@Pipe
def incr(arg):
return arg + 1
print [1, 2, 3] | add | incr
原理就是装饰器+操作符重载.(对装饰器一知半解的请看<python装饰器的本质>)
原文地址:http://blog.csdn.net/handsomekang/article/details/39997849