1. 程式人生 > 實用技巧 >python 函數語言程式設計 左耳聽風

python 函數語言程式設計 左耳聽風

1. pipeline

class Pipe(object):
    def __init__(self, func):
        self.func = func
        print("pipe init")
 
    def __ror__(self, other):
        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)

最終輸出的結果:

先初始化所有的的裝飾器

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())

輸出:

<bclass='bold_css'><iclass='italic_css'>helloworld</i></b>