zoukankan      html  css  js  c++  java
  • Iterables,Generators,Yield

    https://stackoverflow.com/questions/231767/what-does-the-yield-keyword-do?page=1&tab=votes#tab-top

    To understand what yield does, you must understand what generators are. And before you can understand generators, you must understand iterables.

    Iterables

    When you create a list, you can read its items one by one. Reading its items one by one is called iteration:

    >>> mylist = [1, 2, 3]
    >>> for i in mylist:
    ...    print(i)
    1
    2
    3
    

    mylist is an iterable. When you use a list comprehension, you create a list, and so an iterable:

    >>> mylist = [x*x for x in range(3)]
    >>> for i in mylist:
    ...    print(i)
    0
    1
    4
    

    Everything you can use "for... in..." on is an iterable; listsstrings, files...

    These iterables are handy because you can read them as much as you wish, but you store all the values in memory and this is not always what you want when you have a lot of values.

    Generators

    Generators are iterators, a kind of iterable you can only iterate over once. Generators do not store all the values in memory, they generate the values on the fly:

    >>> mygenerator = (x*x for x in range(3))
    >>> for i in mygenerator:
    ...    print(i)
    0
    1
    4
    

    It is just the same except you used () instead of []. BUT, you cannot perform for i in mygenerator a second time since generators can only be used once: they calculate 0, then forget about it and calculate 1, and end calculating 4, one by one.

    Yield

    yield is a keyword that is used like return, except the function will return a generator.

    >>> def create_generator():
    ...    mylist = range(3)
    ...    for i in mylist:
    ...        yield i*i
    ...
    >>> mygenerator = create_generator() # create a generator
    >>> print(mygenerator) # mygenerator is an object!
    <generator object create_generator at 0xb7555c34>
    >>> for i in mygenerator:
    ...     print(i)
    0
    1
    4
    

    Here it's a useless example, but it's handy when you know your function will return a huge set of values that you will only need to read once.

    To master yield, you must understand that when you call the function, the code you have written in the function body does not run. The function only returns the generator object, this is a bit tricky.

    Then, your code will continue from where it left off each time for uses the generator.

    Now the hard part:

    The first time the for calls the generator object created from your function, it will run the code in your function from the beginning until it hits yield, then it'll return the first value of the loop. Then, each subsequent call will run another iteration of the loop you have written in the function and return the next value. This will continue until the generator is considered empty, which happens when the function runs without hitting yield. That can be because the loop has come to an end, or because you no longer satisfy an "if/else".

  • 相关阅读:
    浅谈软件开发项目的质量控制
    分布式系统稳定性模式
    正确使用 Volatile 变量
    我和 OI 的一些故事
    NOIP2020 退役记
    博弈论基础入门
    [HAOI2008]硬币购物(容斥/背包DP)
    [CF] 1307F Cow and Vacation(思维/贪心)
    [noi.ac 模拟赛8] c(容斥/DP)
    [noi.ac 模拟赛9] A.出征准备(同余最短路)
  • 原文地址:https://www.cnblogs.com/yibeimingyue/p/15100489.html
Copyright © 2011-2022 走看看