iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >python之yield表达式
  • 591
分享到

python之yield表达式

表达式pythonyield 2023-01-30 22:01:04 591人浏览 薄情痞子

Python 官方文档:入门教程 => 点击学习

摘要

yield表达式用于generator function 调用generator function时,返回一个iterator(函数内语句不被会执行),调用iterator函数时,执行到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. 

官方例子:

>>> 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函数

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)

 

--结束END--

本文标题: python之yield表达式

本文链接: https://www.lsjlt.com/news/179854.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • python之yield表达式
    yield表达式用于generator function 调用generator function时,返回一个iterator(函数内语句不被会执行),调用iterator函数时,执行到yield表达式, 当前函数暂停执行,返回表达式的值...
    99+
    2023-01-30
    表达式 python yield
  • JSGenerator函数yield表达式示例详解
    目录什么是 Generator 函数yield 表达式yield 表达式和return语句的区别yield* 表达式next() 方法的参数与 Iterator 接口的关系for.....
    99+
    2022-11-13
    JS Generator函数yield表达式 JS Generator 函数
  • JavaScript中Generator函数yield表达式示例详解
    以上就是JavaScript中Generator函数yield表达式示例详解的详细内容,更多请关注编程网其它相关文章!...
    99+
    2022-11-22
    javascript
  • MariaDB表表达式之公用表表达式(CTE)
    目录前言1.非递归CTE2.递归CTE2.1 语法2.2 递归CTE示例(1)2.2 递归CTE示例(2)2.2 递归CTE示例(3)总结前言 公用表表达式(Common Table...
    99+
    2022-11-13
  • Python全栈之正则表达式
    目录1. 正则表达式_匹配单个字符2. 正则表达式_匹配多个字符3. 正则表达式_匹配分组小提示:4. 小练习答案:总结1. 正则表达式_匹配单个字符 正则表达式在线测试: http...
    99+
    2022-11-12
  • python进阶之正则表达式
    概念:   正则表达式是对字符串操作的一种逻辑公式,就是用事先定义好的一些特定字符、及这些特定字符的组合,组成一个“规则字符串”,这个“规则字符串”用来表达对字符串的一种过滤逻辑。 目的?       给定一个正则表达式和另一个字符串,我...
    99+
    2023-01-30
    进阶 正则表达式 python
  • JavaScript中Generator函数和yield表达式怎么使用
    这篇“JavaScript中Generator函数和yield表达式怎么使用”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“J...
    99+
    2023-07-04
  • python模块之re(正则表达式)
    匹配模式 re.ASCII同re.A,对应的内联标识为(a),用于向后兼容。使元字符\w, \W, \b, \B, \d, \D, \s和\S仅匹配ASCII字符。该模式只在string模式下有意义,在byte模式下将被忽略。 re.DE...
    99+
    2023-01-31
    模块 正则表达式 python
  • Python爬虫之正则表达式(1)
    廖雪峰正则表达式学习笔记 1:用\d可以匹配一个数字;用\w可以匹配一个字母或数字; '00\d' 可以匹配‘007’,但是无法匹配‘00A’; ‘\d\d\d’可以匹配‘010’; ‘\w\w\d’可以匹配‘py3’; 2...
    99+
    2023-01-30
    爬虫 正则表达式 Python
  • python--模块之re正则表达式
    简介: 正则表达式本身是一个小型的、高度专业化的编程语言,而在python中,通过内嵌集成re模块,我们可以通过直接调用来实现正则匹配。 正则表达式基础知识: --普通字符匹配自身 abc ----abc --元字符 . :匹...
    99+
    2023-01-30
    模块 正则表达式 python
  • Python函数式编程之lambda表达
    一:匿名函数的定义 lambda parameter_list: expression二:三元表达式 条件为真时返回的结果 if 条件判断 else 条件为假的时候返回的结果三:map map(func(arg1, ar...
    99+
    2023-01-30
    函数 Python lambda
  • Python之yield生成器
    1、对比range和xrange的区别:>>> print range(10) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> print xrange(10) xrange(10)...
    99+
    2023-01-31
    生成器 Python yield
  • 玩转python爬虫之正则表达式
    面对大量杂乱的代码夹杂文字我们怎样把它提取出来整理呢?下面就开始介绍一个十分强大的工具,正则表达式! 1.了解正则表达式 正则表达式是对字符串操作的一种逻辑公式,就是用事先定义好的一些特定字符、及这些特定字...
    99+
    2022-06-04
    爬虫 玩转 正则表达式
  • python之yield与装饰器
    防伪码:忘情公子著python中的yield:  在之前发布的《python之列表解析与生成器》中我们有提到过,生成器所实现的是跟列表解析近似的效果,但是我们不能对生成器做一些属于列表解析的操作。  因为生成器本身就不是一个列表,它只是模拟...
    99+
    2023-01-31
    python yield
  • python正则表达式之作业计算器
    作业:计算器开发 实现加减乘除及拓号优先级解析 用户输入 1 - 2 * ( (60-30 +(-40/5) * (9-2*5/3 + 7 /3*99/4*2998 +10 * 568/14 )) - (-...
    99+
    2022-06-04
    作业 计算器 正则表达式
  • python模块之re正则表达式详解
    一、简单介绍 正则表达式是一种小型的、高度专业化的编程语言,并不是python中特有的,是许多编程语言中基础而又重要的一部分。在python中,主要通过re模块来实现。 正则表达式模式被编译成一系列的字节码...
    99+
    2022-06-04
    详解 模块 正则表达式
  • python数据操作之lambda表达式详情
    目录1 前言2 lambda 的特性3 lambda 的一些用法3.1 map 函数3.2 reduce 函数3.3 sorted 函数3.4 filter 函数4 总结1 前言 在...
    99+
    2022-11-10
  • python爬虫之解析库正则表达式
       上次说到了requests库的获取,然而这只是开始,你获取了网页的源代码,但是这并不是我们的目的,我们的目的是解析链接里面的信息,比如各种属性  @href  @class span  抑或是p节点里面的文本内容,但是我们需要一种工...
    99+
    2023-01-30
    爬虫 正则表达式 python
  • spring之SpEL表达式详解
    目录1.什么是SpEL表达式2.SpEL表达式语言入门程序(1)xml配置的方式(2)采用注解的方式3.分析器4.使用SpEL表达式调用方法(1)使用SpEL调用普通方法(2)使用S...
    99+
    2022-11-13
  • 爬虫之正则表达式
    1.学习爬虫,为什么必须会正则表达式?    我们爬取一些网页具体内容时,只需要这个网页某个标签的一部分内容就足够,或者是这个标签的某个属性的值时,用普通的 xpath 或者css.selector是不能完成的,此时我们就需用到正则表达式...
    99+
    2023-01-30
    爬虫 正则表达式
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作