• Python基础(9)_生成器(yield表达式形式)、面向过程编程


    一、yield表达式形式

     1 #装饰器,初始化含yield表达式的生成器
     2 def init(func):
     3     def wrapper(*args,**kwargs):
     4         g=func(*args,**kwargs)
     5         next(g)
     6         return g
     7     return wrapper
     8 @init
     9 def eater(name):
    10     print('%s ready to eat'%name)
    11     list=[]
    12     while True:
    13         food=yield list
    14         list.append(food)
    15         print('%s had eaten %s'%(name,food))
    16 
    17 e=eater('alex')
    18 print(e.send('鲍鱼'))
    19 print(e.send('鱼翅'))

     含yield表达式的生成器,在使用之前需要初始化,即执行一次net(g),将函数执行停留在yield处

    send的效果:
       1:先从为暂停位置的那个yield传一个值,然后yield会把值赋值x
       2:与next的功能一样,如果yield后面有返回值,则返回该值,没有,返回值为None

    二、os模块,递归取文件绝对路径

    1 import os
    2 g=os.walk(r'D:Pycharm Workspace')   #g为生成器
    3 for par_dir,_,files in g:
    4     for file in files:
    5         file_path=r'%s\%s'%(par_dir,file)
    6         print(file_path)

    三、面向过程编程

    面向过程编程又叫函数式编程:

      函数是Python内建支持的一种封装,我们通过把大段代码拆成函数,通过一层一层的函数调用,就可以把复杂任务分解成简单的任务,这种分解可以称之为面向过程的程序设计。函数就是面向过程的程序设计的基本单元。

    核心:是过程,过程即流程

    优点:思路清晰,复杂的问题简单化,流程化

    缺点:扩展性差

    应用:linux内核,httpd,git

     1 #模拟 grep ‘root’ /etc功能
     2 import os
     3 def init(func):
     4     def wrapper(*args,**kwargs):
     5         g=func(*args,**kwargs)
     6         next(g)
     7         return g
     8     return wrapper
     9 #阶段一:递归地找文件的绝对路径,把路径发给阶段二
    10 @init
    11 def search(target):
    12     'search file abspath'
    13     while True:
    14         start_path=yield
    15         g = os.walk(start_path)
    16         for par_dir, _, files in g:
    17             # print(par_dir,files)
    18             for file in files:
    19                 file_path = r'%s\%s' % (par_dir, file)
    20                 target.send(file_path)
    21 #阶段二:收到文件路径,打开文件获取获取对象,把文件对象发给阶段三
    22 @init
    23 def opener(target):
    24     'get file obj: f=open(filepath)'
    25     while True:
    26         file_path=yield
    27         with open(file_path,encoding='utf-8') as f:
    28             target.send((file_path,f))
    29 
    30 #阶段三:收到文件对象,for循环读取文件的每一行内容,把每一行内容发给阶段四
    31 @init
    32 def cat(target):
    33     'read file'
    34     while True:
    35         filepath,f=yield
    36         for line in f:
    37             res=target.send((filepath,line))
    38             if res:
    39                 break
    40 
    41 #阶段四:收到一行内容,判断root是否在这一行中,如果在,则把文件名发给阶段五
    42 @init
    43 def grep(target,pattern):
    44     'grep function'
    45     tag=False
    46     while True:
    47         filepath,line=yield tag #target.send((filepath,line))
    48         tag=False
    49         if pattern in line:
    50             target.send(filepath)
    51             tag=True
    52 #阶段五:收到文件名,打印结果
    53 @init
    54 def printer():
    55     'print function'
    56     while True:
    57         filename=yield
    58         print(filename)
    59 
    60 start_path = r'D:Pycharm WorkspaceDay10'
    61 g=search(opener(cat(grep(printer(),'root'))))
    62 
    63 print(g)
    64 # g.send(start_path1)
    65 g.send(start_path)

    面向过程编程核心是过程,缺点是扩展性差

    from峰哥:  

      函数的参数传入,是函数吃进去的食物,而函数return的返回值,是函数拉出来的结果,面向过程的思路就是,把程序的执行当做一串首尾相连的函数,一个函数吃,拉出的东西给另外一个函数吃,另外一个函数吃了再继续拉给下一个函数吃。。。

    例如:
      用户登录流程:前端接收处理用户请求-》将用户信息传给逻辑层,逻辑词处理用户信息-》将用户信息写入数据库
      验证用户登录流程:数据库查询/处理用户信息-》交给逻辑层,逻辑层处理用户信息-》用户信息交给前端,前端显示用户信息




  • 相关阅读:
    my sql Group_concat函数
    [Effective JavaScript 笔记]第43条:使用Object的直接实例构造轻量级的字典
    [Effective JavaScript 笔记]第4章:对象和原型--个人总结
    [Effective JavaScript 笔记]第42条:避免使用轻率的猴子补丁
    [Effective JavaScript 笔记]第41条:将原型视为实现细节
    [Effective JavaScript 笔记]第40条:避免继承标准类
    [Effective JavaScript 笔记]第39条:不要重用父类的属性名
    [Effective JavaScript 笔记]第38条:在子类的构造函数中调用父类的构造函数
    [Effective JavaScript 笔记]第37条:认识到this变量的隐式绑定问题
    [Effective JavaScript 笔记]第36条:只将实例状态存储在实例对象中
  • 原文地址:https://www.cnblogs.com/hedeyong/p/7049405.html
Copyright © 2020-2023  润新知