python 函数式编程 左耳听风

 

 

1. pipeline 

class Pipe(object):
    def __init__(self, func):
        self.func = func
        print("pipe init")
 
    def __ror__(self, other): //再执行或的时候,调用装饰器当中的函数。因为int.__or__不知道如何使用的实例MyInt在这种情况下,Python会交换操作数的顺序并尝试
        def generator():
            for obj in other:
                if obj is not None:
                    print("yield begin obj {} fun {} ".format(obj,self.func.__name__))
                    yield self.func(obj)
                    print("yield end obj {} fun {} ".format(obj,self.func.__name__))
                else:
                    print("yield end obj is null, fun {} ".format(self.func.__name__))
        return generator()
 
此处会被解释为 Pipe(event_filter)
@Pipe
def even_filter(num):
    return num if num % 2 == 0 else None
 
@Pipe
def multiply_by_three(num):
    return num*3
 
@Pipe
def convert_to_string(num):
    return 'The Number: %s' % num
 
@Pipe
def echo(item):
    print(item)
    return item
 
def force(sqs):
    for item in sqs: pass

nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
 
# 这个里面定义的函数会顺序执行
force(nums | even_filter | multiply_by_three | convert_to_string | echo)
nums|event_filere 返回了一个生产器a,
a | mutiplay_by_three 返回生成器b,
b |convertt_to_string 返回生成器c,
c |echo 返回生成器d。
最后是force的循环先执行生成器d, 然后在c, 然后在b,然后在a, 最后是nums, 取到数据就依次返回。返回后,进入下一次的迭代。

 

最终输出的结果:

先初始化所有的的装饰器

pipe init func {} even_filter
pipe init func {} multiply_by_three
pipe init func {} convert_to_string
pipe init func {} echo

 

顺序迭代每一个字段,如果无效,就跳过
yield begin obj 1 fun even_filter
yield end obj is null, fun multiply_by_three
yield end obj 1 fun even_filter

如果合适,就执行每一个函数。
yield begin obj 2 fun even_filter
yield begin obj 2 fun multiply_by_three
yield begin obj 6 fun convert_to_string
yield begin obj The Number: 6 fun echo
The Number: 6


yield end obj The Number: 6 fun echo
yield end obj 6 fun convert_to_string
yield end obj 2 fun multiply_by_three
yield end obj 2 fun even_filter
yield begin obj 3 fun even_filter
yield end obj is null, fun multiply_by_three
yield end obj 3 fun even_filter
yield begin obj 4 fun even_filter
yield begin obj 4 fun multiply_by_three
yield begin obj 12 fun convert_to_string
yield begin obj The Number: 12 fun echo
The Number: 12
yield end obj The Number: 12 fun echo
yield end obj 12 fun convert_to_string
yield end obj 4 fun multiply_by_three
yield end obj 4 fun even_filter
yield begin obj 5 fun even_filter
yield end obj is null, fun multiply_by_three
yield end obj 5 fun even_filter
yield begin obj 6 fun even_filter
yield begin obj 6 fun multiply_by_three
yield begin obj 18 fun convert_to_string
yield begin obj The Number: 18 fun echo
The Number: 18
yield end obj The Number: 18 fun echo
yield end obj 18 fun convert_to_string
yield end obj 6 fun multiply_by_three
yield end obj 6 fun even_filter
yield begin obj 7 fun even_filter
yield end obj is null, fun multiply_by_three
yield end obj 7 fun even_filter
yield begin obj 8 fun even_filter
yield begin obj 8 fun multiply_by_three
yield begin obj 24 fun convert_to_string
yield begin obj The Number: 24 fun echo
The Number: 24
yield end obj The Number: 24 fun echo
yield end obj 24 fun convert_to_string
yield end obj 8 fun multiply_by_three
yield end obj 8 fun even_filter
yield begin obj 9 fun even_filter
yield end obj is null, fun multiply_by_three
yield end obj 9 fun even_filter
yield begin obj 10 fun even_filter
yield begin obj 10 fun multiply_by_three
yield begin obj 30 fun convert_to_string
yield begin obj The Number: 30 fun echo
The Number: 30
yield end obj The Number: 30 fun echo
yield end obj 30 fun convert_to_string
yield end obj 10 fun multiply_by_three
yield end obj 10 fun even_filter

 

2. 装饰器

def makeHtmlTag(tag, *args, **kwds):# python *args 称作为数组参数,**kwargs 称作为字典参数 ,这个技巧可以用下
    def real_decorator(fn):
        css_class = " class='{0}'".format(kwds["css_class"]) if "css_class" in kwds else ""  #如果有css_class 执行前面的,如果没有就是空,这个写法好玩 ( action_ture if condition else action_false)
        def wrapped(*args, **kwds):
            return "<"+tag+css_class+">" + fn(*args, **kwds) + "</"+tag+">"
        return wrapped
    return real_decorator
 
@makeHtmlTag(tag="b", css_class="bold_css") # 在处理外部的
@makeHtmlTag(tag="i", css_class="italic_css") # 先处理内部的
def hello():
    return "hello world"
 
print (hello())

输出:

 <b class='bold_css'><i class='italic_css'>hello world</i></b>

posted on 2020-10-09 18:40  KHacker  阅读(131)  评论(0编辑  收藏  举报