yield 1,叫做 yield 语句形式
  x = yield , 叫做 yield 表达式形式 --> 协成函数

def eater(name):
    print('%s ready to eat' % name)
    while True:
        food = yield
        print('%s start to eat %s ' %(name,food))

g = eater('Alex') #产生生成器
g.send('Apple') #会触发函数的执行,并传递至给yield。

注意:
  1)表达式形式的生成器,必须先使用next(),或者send(None),才可以让生成器执行。(生成器初始化)
  2)后续可以使用next,但是无法传值,使用send 就可以传值

 

编写装饰器为 yield 表达式形式进行初始化

            def gen(func):
                def wapper(*args,**kwargs):
                    g = func(*args,**kwargs)
                    g.send(None)
                    return g
                return wapper


            @gen
            def eater(name):
                print('%s ready to eat ' % name )
                while True:
                    food = yield #这里的yield可以设置返回值
                    print('%s want to eat %s' %(name,food))


            g = eater('daxin')
            g.send('Apple')
            解读:

解读:

  1)g.send('111'),先把111传给 yield ,由 yield 赋值给food,然后再往下执行,直到再次遇到yield后,停止
  2)可以在 yield 后面设置返回值(比如,添加菜单,然后返回)

应用:grep -rl 'python' /root

       import os

            def init(func):
                def wapper(*args,**kwargs):
                    g = func(*args,**kwargs)
                    next(g)
                    return g
                return wapper

            @init
            def search(target):
                dirpath = yield
                src_file_path = os.walk(dirpath)
                while True:
                    for roots,_,files in src_file_path:
                        for file in files:
                            filepath = "%s/%s" % (roots,file)
                            target.send(filepath)

            @init
            def opener(target):
                while True:
                    file_abs_path = yield
                    with open(file_abs_path,encoding='utf-8') as filename:
                        target.send((filename,file_abs_path))

            @init
            def cat(target):
                while True:
                    filename,file_abs_path = yield
                    for line in filename:
                        tag = target.send((line,file_abs_path))
                        if tag:
                            break

            @init
            def grep(key,target):
                tag = False
                while True:
                    line,file_abs_path = yield tag
                    if key in line:
                        tag = True
                        target.send(file_abs_path)
                    else:
                        tag = False

            @init
            def printer():
                while True:
                    file_abs_path = yield
                    print(file_abs_path)


            if __name__ == '__main__':
                dirpath = r'E:\StartPython\day7'
                keyname = 'socket'
                g = search(opener(cat(grep(keyname,printer()))))
                print(g)
                g.send(dirpath)

 

面向过程的程序设计,是一种流水线式的编程思路,是机械式的
  优点:
    1)程序结构清晰
  缺点:
    2)扩展性差
应用场景:
  1)Linux就是一种面向过程编写的(C不支持面向对象)
  2)git
  3)httpd

匿名函数


  没有名字的函数。适合比较简单的使用场景,函数体必须是返回值
  lambda x,y: x+y
  使用场景:
  1)不需要名字的地方

 

内置函数(补充)


  salaries = {'egon':3000,'alex':10000000,'wupeiqi':100000,'yuanhao':2000}
  zip(iter1,iter2) 拉链函数,每次分别从iter1,iter2 取一个元素,组成元素,最后返回一个迭代器

  def func(k):
    return salaries[k]
View Code

相关文章:

  • 2022-12-23
  • 2021-08-18
  • 2021-11-27
  • 2021-09-05
  • 2022-01-18
  • 2022-02-19
  • 2022-12-23
猜你喜欢
  • 2021-08-20
  • 2021-04-02
  • 2022-12-23
  • 2021-10-06
  • 2021-12-15
  • 2021-09-23
相关资源
相似解决方案