yield for python

a example of code

def fun():
    for i in range(20):
        x = yield i
        print('good', x, i)

if __name__ == '__main__':
    a = fun()
    a.__next__()
    a.__next__()
    x1 = a.send(5)
    x2 = a.send(3)
    next(a)
    next(a)
    print(x1, x2)

results

good None 0
good 5 1
good 3 2
good None 3
good None 4
2 3

description

yield generator functions allow you to declare a function that behaves like an iterator.

recent article

Template

emacs vim blogread
previous article

A script for download all papers on ICML conference

script code# coding=utf-8from multiprocessing import Poolimport requestsfrom bs4 import BeautifulSoupimport tracebackimport reimport osimport pdbprefix = 'http://proceedings.mlr.press/v80/'save_dir = 'icml2018'def get_pdf(data): href, title = d...…

computer science tool script blogread