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
    
    
    
  • 相关阅读:
    【luogu P4139】 上帝与集合的正确用法
    pb_ds学习
    【luogu P3868】 [TJOI2009]猜数字
    BZOJ3040: 最短路(road)
    【luogu P1064】 金明的预算方案
    【luogu P2893】 [USACO08FEB]修路Making the Grade
    【luogu P2801】 教主的魔法
    UVA10816 Travel in Desert
    P2916 [USACO08NOV]安慰奶牛Cheering up the Cow
    【BZOJ 2054】 疯狂的馒头
  • 原文地址:https://www.cnblogs.com/amize/p/14582528.html
Copyright © 2011-2022 走看看