Python doc v2.7.6 ------ 5.2.10.1. Generator-iterator methods

来源:互联网 发布:java课程设计小游戏 编辑:程序博客网 时间:2024/05/20 16:08

This subsection describes the methods of a generator iterator. They can be used to control the execution of a generator function.

Note that calling any of the generator methods below when the generator is already executing raises a ValueError exception.

class generator
generator.next()

Starts the execution of a generator function or resumes it at the last executed yield expression. When a generator function is resumed with a next() method, the current yield expression always evaluates to None. The execution then continues to the next yield expression, where the generator is suspended again, and the value of the expression_list is returned to next()‘s caller. If the generator exits without yielding another value, a StopIteration exception is raised.

开始执行生成器方法或唤醒上一次执行 yield 表达式后被挂起的生成器。当一个生成器方法被用 next() 方法恢复时,当前的 yield 表达式总且一定为 None。方法继续执行,直到下一个 yield 表达式,方法再次被挂起。返回值被调用 next() 的 caller 返回。如果生成器返回时没有yield一个值,会引发 StopIteration 异常。
generator.send(value)

Resumes the execution and “sends” a value into the generator function. The value argument becomes the result of the current yield expression. The send() method returns the next value yielded by the generator, or raises StopIteration if the generator exits without yielding another value. When send() is called to start the generator, it must be called with None as the argument, because there is no yield expression that could receive the value.


★ 恢复方法的执行并给方法 sends 一个值。参数value 成为当前 yield 的结果。send()方法返回下一个被生成器 yield 的值,或者当方法没有yield 一个值并且退出时,引起一个 StopIteration 异常。当用 send() 方法来第一次调用方法时,参数必须为 None ,因为没有 yield 表达式能够接受这个参数。

generator.throw(type[value[traceback]])

Raises an exception of type type at the point where generator was paused, and returns the next value yielded by the generator function. If the generator exits without yielding another value, a StopIteration exception is raised. If the generator function does not catch the passed-in exception, or raises a different exception, then that exception propagates to the caller.

★ 引发一个给定的异常,返回下一个yield的值。如果生成器没有 yield 一个值,会引发 StopIteration 异常。如果生成器没有捕捉给定得异常或引发了不同于参数里给定异常的异常,这个异常会被抛给 caller。

generator.close()

Raises a GeneratorExit at the point where the generator function was paused. If the generator function then raises StopIteration (by exiting normally, or due to already being closed) or GeneratorExit (by not catching the exception), close returns to its caller. If the generator yields a value, a RuntimeError is raised. If the generator raises any other exception, it is propagated to the caller. close() does nothing if the generator has already exited due to an exception or normal exit.

★ 引起一个 GeneratorExit 异常。


Here is a simple example that demonstrates the behavior of generators and generator functions:

>>> def echo(value=None):...     print "Execution starts when 'next()' is called for the first time."...     try:...         while True:...             try:...                 value = (yield value)...             except Exception, e:...                 value = e...     finally:...         print "Don't forget to clean up when 'close()' is called."...>>> generator = echo(1)>>> print generator.next()Execution starts when 'next()' is called for the first time.1>>> print generator.next()None>>> print generator.send(2)2>>> generator.throw(TypeError, "spam")TypeError('spam',)>>> generator.close()Don't forget to clean up when 'close()' is called.


0 0
原创粉丝点击