zoukankan      html  css  js  c++  java
  • 生成器

    • 生成器
    • from collections import Iterator
    • def test():
    •     print("one")
    •     yield 1
    •     print("two")
    •     yield 2
    •     print("three")
    •     yield 3
    •  
    •  
    • obj = test()
    •  
    • res next(obj)
    • print(res)
    •     # one
    •     # 1
    • res next(obj)
    • print(res)
    •     # two
    •     # 2
    • res next(obj)
    • print(res)
    •     # three
    •     # 3
    •  
    • --------------------------
    • from collections import Iterator
    • def test():
    •     print("one")
    •     yield 1
    •     print("two")
    •     yield 2
    •     print("three")
    •     yield 3
    •  
    •  
    • obj = test()
    • next(obj)
    •     #one
    • next(obj)
    •     #two
    • next(obj)
    •     #three
      • 概念
        • 可以理解为一种数据类型,这种数据类型自动实现了迭代器协议(其他的数据类型需要调用自己内置的__iter__方法),所以生成器就是可迭代对象 
      • 特征
        • 1. yield 把函数变成生成器,生成器就是迭代器 
        • def test():
        •     print("one")
        •     yield 1
        •     print("two")
        •     yield 2
        •     print("three")
        •     yield 3
        •  
        • obj = test()
        • for i in obj:
        •     print(i)
        •     # one
        •     # 1
        •     # two
        •     # 2
        •     # three
        •     # 3
        • 2. 生成器就是一个函数,这个函数内包含有yeild这个关键字
        • 3. 把函数做成一个迭代器,函数可以每次生成一个值
      • 分类
        • 生成器函数
          • 使用yield语句一次返回一个结果,在每个结果中间,挂起函数的状态,以便下次从它离开的地方继续执行
        • 生成器表达式
          • 类似于列表推导,但是,生成器返回按需产生结果的一个对象,而不是一次构建一个结果列表
      • 优点
        • Python使用生成器对延迟操作提供了支持。所谓延迟操作,是指在需要的时候才产生结果,而不是立即产生结果。这也是生成器的主要好处
      • yield与return区别
        • yield 可以返回多个值
        • return 只能返回一次函数就结束了
      • yield功能
        • 1.相当于把__iter__和__next__方法封装到函数内部
        • 2.与return比,return只能返回一次,而yield能返回多次
        • 3.函数暂停已经继续运行的状态是通过yield保存的
      • .send .next
        • 1.如果函数内yeild是表达式形式,那么必须先next(e)
        • 2.二者的共同之处都可以让函数在上次暂时的位置继续运行不同之处在于send在出发下一次的执行时,会顺便给yield传一个值
      • 事例
        • count
        • def countdown(count):
        •     print("start")
        •     while count > 0:
        •         yield count
        •         count -= 1
        •     print("end")
        •  
        • obj = countdown(5)
        • for i in obj:
        •     print(i)
        •  
        •  
        • obj = countdown(5)
        • while True:
        •     try:
        •         print(next(obj))
        •     except Exception:
        •         break
        •  
        • #------------------
        • start
        • 5
        • 4
        • 3
        • 2
        • 1
        • end
        • tail
        • #/usr/bin/env python
        • import time
        • #定义阶段:定义俩生成器函数
        • def tail(file_path):
        •     with open(file_path,'r') as f:
        •         f.seek(0,2)
        •         while True:
        •             line=f.readline()  #最后一行
        •   
        •             if not line:
        •                 time.sleep(0.3)
        •                 continue
        •             else:
        •                 #print(line,end='')
        •                 yield line
        •   
        • def grep(pattern,lines):
        •     for line in lines:
        •         if pattern in line:
        •             yield line
        •   
        • #调用阶段:得到俩生成器对象
        • g1=tail('/tmp/a.txt')
        • g2=grep('error',g1)
        •   
        • #next触发执行g2生成器函数
        • for i in g2:
        •     print(i)
        •   
        • tail -/tmp/a.txt | grep error
  • 相关阅读:
    sublime text 3安装 package control 插件的方法
    mysql 库和表占用空间查询
    错误:编码GBK的不可映射字符
    新系统设置 github 私钥
    git 之忽略文件 gitignore 创建和使用规则
    DesiredCapabilities内容详解(摘)
    appium-python自动化之get_attribute笔记(摘)
    excel批量插入图片-方法(宏和纯手工操作)
    利用Python将多个excel文件合并为一个文件
    notePad++ 运行python
  • 原文地址:https://www.cnblogs.com/golangav/p/7294310.html
Copyright © 2011-2022 走看看