zoukankan      html  css  js  c++  java
  • 为什么要用yield

    https://www.runoob.com/w3cnote/python-yield-used-analysis.html

    1. 演进
    
    yield 的作用就是把一个函数变成一个 generator,带有 yield 的函数不再是一个普通函数,Python 解释器会将其视为一个 generator,调用 fab(5) 不会执行 fab 函数,
    而是返回一个 iterable 对象!在 for 循环执行时,每次循环都会执行 fab 函数内部的代码,执行到 yield b 时,fab 函数就返回一个迭代值,下次迭代时,
    代码从 yield b 的下一条语句继续执行,而函数的本地变量看起来和上次中断执行前是完全一样的,于是函数继续执行,直到再次遇到 yield。
    生成一个 generator 看起来像函数调用,但不会执行任何函数代码,直到对其调用 next()(在 for 循环中会自动调用 next())才开始执行。
    在一个 generator function 中,如果没有 return,则默认执行至函数完毕,如果在执行过程中 return,则直接抛出 StopIteration 终止迭代。
    
    
    #!/usr/bin/python
    # -*- coding: UTF-8 -*-
     
    def fab(max): 
        n, a, b = 0, 0, 1 
        while n < max: 
            print b 
            a, b = b, a + b 
            n = n + 1
    fab(5)
    
    
    
    
    #!/usr/bin/python
    # -*- coding: UTF-8 -*-
     
    def fab(max): 
        n, a, b = 0, 0, 1 
        L = [] 
        while n < max: 
            L.append(b) 
            a, b = b, a + b 
            n = n + 1 
        return L
     
    for n in fab(5): 
        print n
    
    
    
    
    
    #!/usr/bin/python
    # -*- coding: UTF-8 -*-
     
    class Fab(object): 
     
        def __init__(self, max): 
            self.max = max 
            self.n, self.a, self.b = 0, 0, 1 
     
        def __iter__(self): 
            return self 
     
        def next(self): 
            if self.n < self.max: 
                r = self.b 
                self.a, self.b = self.b, self.a + self.b 
                self.n = self.n + 1 
                return r 
            raise StopIteration()
     
    for n in Fab(5): 
        print n
    
    
    
    
    
    # 第四个版本的 fab 和第一版相比,仅仅把 print b 改为了 yield b,就在保持简洁性的同时获得了 iterable 的效果。
    # 调用第四版的 fab 和第二版的 fab 完全一致:
    #!/usr/bin/python
    # -*- coding: UTF-8 -*-
     
    def fab(max): 
        n, a, b = 0, 0, 1 
        while n < max: 
            yield b      # 使用 yield
            # print b 
            a, b = b, a + b 
            n = n + 1
     
    for n in fab(5): 
        print n
    
    
    
    
    另一个 yield 的例子来源于文件读取。如果直接对文件对象调用 read() 方法,会导致不可预测的内存占用。好的方法是利用固定长度的缓冲区来不断读取文件内容。
    通过 yield,我们不再需要编写读文件的迭代类,就可以轻松实现文件读取:
    def read_file(fpath): 
        BLOCK_SIZE = 1024 
        with open(fpath, 'rb') as f: 
            while True: 
                block = f.read(BLOCK_SIZE) 
                if block: 
                    yield block 
                else: 
                    return
    
    
    
  • 相关阅读:
    MongoDB 2.4、备份
    MongoDB 2.3复制(副本集)
    MongoDB 2.2安全
    isMobile 一个简单的JS库,用来检测移动设备
    修改ECSHOP,支持图片云存储化(分离到专用图片服务器)
    压缩代码加速ecshop程序页面加载速度
    ecshop改造读写分离
    ecshop在nginx下实现负载均衡
    运用@media实现网页自适应中的几个关键分辨率
    在ECSHOP首页今日特价(促销商品)增加倒计时效果
  • 原文地址:https://www.cnblogs.com/amize/p/14582528.html
Copyright © 2011-2022 走看看