python中yield表达式的简单介绍(附示例)


本文摘自php中文网,作者不言,侵删。

本篇文章给大家带来的内容是关于python中yield表达式的简单介绍(附示例),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。

yield表达式用于generator function

调用generator function时,返回一个iterator(函数内语句不被会执行),调用iterator函数时,执行到yield表达式,

当前函数暂停执行,返回表达式的值到调用者,继续调用iterator函数,从暂停处恢复执行。、

遇到yield表达式,与遇到其他表达式差不多,yield表达式也有值,一般为None。

与其他表达式的不同之处在于yield表达式会在yield处返回表达式的值

官方文档描述如下:

When a generator function is called, it returns an iterator known as a generator. That generator then controls the execution of the generator function. The execution starts when one of the generator’s methods is called. At that time, the execution proceeds to the first yield expression, where it is suspended again, returning the value of expression_list to the generator’s caller. By suspended, we mean that all local state is retained, including the current bindings of local variables, the instruction pointer, the internal evaluation stack, and the state of any exception handling. When the execution is resumed by calling one of the generator’s methods, the function can proceed exactly as if the yield expression were just another external call. The value of the yield expression after resuming depends on the method which resumed the execution. If __next__() is used (typically via either a for or the next() builtin) then the result is None. Otherwise, if send() is used, then the result will be the value passed in to that method.

Yield expressions are allowed anywhere in a try construct. If the generator is not resumed before it is finalized (by reaching a zero reference count or by being garbage collected), the generator-iterator’s close() method will be called, allowing any pending finally clauses to execute.

When the underlying iterator is complete, the value attribute of the raised StopIteration instance becomes the value of the yield expression.

官方例子:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

>>> def echo(value=None):

...     print("Execution starts when 'next()' is called for the first time.")

...     try:

...         while True:

...             try:

...                 value = (yield value)

...             except Exception as e:

...                 value = e

...     finally:

...         print("Don't forget to clean up when 'close()' is called.")

...>>> generator = echo(1)>>> print(next(generator))

Execution starts when 'next()' is called for the first time.1

>>> print(next(generator))

None>>> print(generator.send(2))2

>>> generator.throw(TypeError, "spam")

TypeError('spam',)>>> generator.close()

Don't forget to clean up when 'close()' is called.

模拟个iterator版range函数

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

def my_range(start, stop=None, step=1):

    if not stop:

        stop = start

        start = 0

    while start < stop:

        yield start

        start += step

 

 

if __name__ == '__main__':

    for i in my_range(10):

        print(i)

    for i in my_range(0, 10):

        print(i)

    for i in my_range(0, 10, 2):

        print(i)

以上就是python中yield表达式的简单介绍(附示例)的详细内容,更多文章请关注木庄网络博客!!

相关阅读 >>

Python怎么卸载模块

Python中关于flask部署后并发测试的示例代码

Python语言的特点有哪些

Python中关于list()列表的具体分析

了解为什么说 Python 内置函数并不是万能的?

redis缓存服务器在django中的用法详解

Python怎么输出表格

Python中队列的实现方法(代码示例)

selenium之实现自动登录的实例代码

Python中reduce()函数的示例

更多相关阅读请进入《Python》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...