潘飞

潘飞的博客

他的个人主页  他的博客

Yield

潘飞  2009年11月11日 星期三 11:32 | 1489次浏览 | 6条评论

Specification: Yield

 A new statement is introduced:
 

yield_stmt: "yield" expression_list

"yield" is a new keyword, so a future statement[8] is needed to phase
this in: in the initial release, a module desiring to use generators
must include the line

from __future__ import generators

near the top (see PEP 236 [8]) for details). Modules using the
identifier "yield" without a future statement will trigger warnings.
In the following release, yield will be a language keyword and the
future statement will no longer be needed.

The yield statement may only be used inside functions. A function that
contains a yield statement is called a generator function. A generator
function is an ordinary function object in all respects, but has the
new CO_GENERATOR flag set in the code object's co_flags member.

When a generator function is called, the actual arguments are bound to
function-local formal argument names in the usual way, but no code in
the body of the function is executed. Instead a generator-iterator
object is returned; this conforms to the iterator protocol[6], so in
particular can be used in for-loops in a natural way. Note that when
the intent is clear from context, the unqualified name "generator" may
be used to refer either to a generator-function or a generator-
iterator.

Each time the .next() method of a generator-iterator is invoked, the
code in the body of the generator-function is executed until a yield
or return statement (see below) is encountered, or until the end of
the body is reached.

If a yield statement is encountered, the state of the function is
frozen, and the value of expression_list is returned to .next()'s
caller. By "frozen" we mean that all local state is retained,
including the current bindings of local variables, the instruction
pointer, and the internal evaluation stack: enough information is
saved so that the next time .next() is invoked, the function can
proceed exactly as if the yield statement were just another external
call.

Restriction: A yield statement is not allowed in the try clause of a
try/finally construct. The difficulty is that there's no guarantee
the generator will ever be resumed, hence no guarantee that the finally
block will ever get executed; that's too much a violation of finally's
purpose to bear.

Restriction: A generator cannot be resumed while it is actively
running:

>>> def g():
... i = me.next()
... yield i
>>> me = g()
>>> me.next()
Traceback (most recent call last):
...
File "<string>", line 2, in g
ValueError: generator already executing


评论

我的评论:

发表评论

请 登录 后发表评论。还没有在Zeuux哲思注册吗?现在 注册 !
杨延平

回复 杨延平  2009年11月12日 星期四 11:32

???垃圾贴?

5条回复

  • 潘飞

    回复 潘飞  2009年11月12日 星期四 11:34

    所以想分享一下

    0条回复

  • 潘飞

    回复 潘飞  2009年11月12日 星期四 11:34

    恰相反,我通过这段内容,把generator给掌握了,呵呵

    3条回复

      • 杨延平

        回复 杨延平  2009年11月12日 星期四 16:53

        分享后你觉得你的文章可读性如何
        把E文重点摘出来吧。
        嗯。没提到 k = (yield) 的语法

        2条回复

          • 潘飞

            回复 潘飞  2009年11月12日 星期四 16:55

            关键是自己去做,我看完了之后,自己实现了XRange类,至少在认识上有了提高,你说呢?

            1条回复

暂时没有评论

Zeuux © 2024

京ICP备05028076号