zoukankan      html  css  js  c++  java
  • yield

    generator归根到底是一个函数的返回值,这个函数是包含“yield”关键字的python函数。

    是不是可以这么说(不是很确定,似乎可以这么理解)
    1,凡包含“yield”关键字的函数,都返回generator
    2,generator不是函数,而是函数执行后构造的对象,是一种iterator。
    3,generator可以像iterator一样的用。

    generator的根源是PEP 255,其中列出了generator在Python存在的原因,简单的讲,Generator在需要时返回中间值,能够保存当前的状态,等待下一次的返回要求。

    xrange/range的区别或许可以帮我们理解这一点,xrange之所以存在,是因为range需要一次完成列表的初始化,存储等等,从C的角度来理解,就是,用range等于先malloc足够的内存,然后完成值的准备,等待调用(遍历等等)。而xrange则不这么干,什么时候要的时候,什么时候给值。所以,在Python 2.x中,type(range(10))是一个List,是内存中的静态数据;而type(xrange(10))则是一个range type。

    到Python 3.x,xrange彻底替代了range函数。

    这样设计的目的无非就是节省内存 ,千八百数字的无所谓,但ython 2.x的long int和Python 3.x的Int是无限制(用官方语言来说就是可以占满内存)。

    generator为了满足这种需求设计的,状态得到了保存,随取随算。

    PEP 255有一句: a Python generator is a kind of Python iterator[1], but of an especially powerful kind.

    Python的产生器就是一种迭代器...
    因为它是一种迭代器,所以,他可以用到for等控制流中。

    def gen():
        yield 1
        yield 2
        yield 3
    print(type(gen))    #<class 'function'>
    print(type(gen())) #<class 'generator'>

    可以看到gen是函数,而gen()是generator,应该说,函数gen执行的返回值是生成一个generator。

    generator的方法之一就是next()。

    # __author__ = liukun
    # coding:utf-8
    def gen():
        yield 1
        yield 2
        yield 3
    print(type(gen))    #<class 'function'>
    print(type(gen())) #<class 'generator'>
    a=gen()
    print(a.__next__())
    # generator(有yield关键字的函数则会被识别为generator函数)中的next变为__next__了,next是python 3.x以前版本中的方法
    print(a.__next__()) print(a.__next__())

    '''

    /Library/Frameworks/Python.framework/Versions/3.5/bin/python3.5 /Applications/PyCharm.app/Contents/helpers/pycharm/utrunner.py /Users/kamil/PycharmProjects/untitled4/Day5/python基础教程/test.py true
    Testing started at 下午3:40 ...
    <class 'function'>
    <class 'generator'>
    1
    2
    3

    Process finished with exit code 0
    Empty test suite.


    '''

    而yield的作用就是,每次发生next()调用,函数执行完yield语句之后在挂起,这时返回yield的值(你愿意yield啥就yield啥),整个函数状态被保存,等待下一次next()调用;
    下次next()调用发生时,从yield后的语句开始执行(有yiled也在循环体内,未必一定是顺序的),直到再次遇到yield为止,然后重复删除动作。

    yield 可以解读为"返回然后等待"。直到所有yield语句完成,这时如果再次调用next(),则发生StopIteration异常,当然,在for循环之类的语句中会被自动处理。

    公众号请关注:侠之大者
  • 相关阅读:
    BestCoder6 1002 Goffi and Squary Partition(hdu 4982) 解题报告
    codeforces 31C Schedule 解题报告
    codeforces 462C Appleman and Toastman 解题报告
    codeforces 460C. Present 解题报告
    BestCoder3 1002 BestCoder Sequence(hdu 4908) 解题报告
    BestCoder3 1001 Task schedule(hdu 4907) 解题报告
    poj 1195 Mobile phones 解题报告
    二维树状数组 探索进行中
    codeforces 460B Little Dima and Equation 解题报告
    通过Sql语句控制SQLite数据库增删改查
  • 原文地址:https://www.cnblogs.com/kamil/p/5199479.html
Copyright © 2011-2022 走看看