当前位置: 首页 > news >正文

yield and generator in python

首先,假设大家都对于pytyhon的List comprehension的使用有了一定经验(它可以用于list,set,和dict哦)

不熟悉的参考介绍: Comprehending Python’s Comprehensions – dbader.org

generator

generator是哦嗯一种返回lazy iterator的特殊类型函数。list comprehensions return full lists, while generator expressions return generators.

如下面代码,分别使用list和generator的构造,但是两者所占据的内存大家却完全不一样。

>>> import sys
>>> nums_squared_lc = [i ** 2 for i in range(10000)]
>>> sys.getsizeof(nums_squared_lc)
87624
>>> nums_squared_gc = (i ** 2 for i in range(10000))
>>> print(sys.getsizeof(nums_squared_gc))
120

 generator functions are a special kind of function that return a lazy iterator. These are objects that you can loop over like a list. However, unlike lists, lazy iterators do not store their contents in memory.

在python中,通过“类似list comprehension”或者yield的使用,我们可以构造generator并调用。

yield

yield的作用介绍如下,当使用generator的特殊方法,比如next()时,generator中的代码会被执行到yield,当经过yield时,python中generator的执行会被中断,函数的当前状态会被保存,直到下一次执行。 

下列函数是yield的一个经典用法。

def infinite_sequence():num = 0while True:yield numnum += 1

在调用该函数的过程中,每一次,输出的数会增加1,相比于return,yield会保存function当前的状态,参考How to Use Generators and yield in Python – Real Python

On the whole, yield is a fairly simple statement. Its primary job is to control the flow of a generator function in a way that’s similar to return statements. As briefly mentioned above, though, the Python yield statement has a few tricks up its sleeve.

When you call a generator function or use a generator expression, you return a special iterator called a generator. You can assign this generator to a variable in order to use it. When you call special methods on the generator, such as next(), the code within the function is executed up to yield.

When the Python yield statement is hit, the program suspends function execution and returns the yielded value to the caller. (In contrast, return stops function execution completely.) When a function is suspended, the state of that function is saved. This includes any variable bindings local to the generator, the instruction pointer, the internal stack, and any exception handling.

This allows you to resume function execution whenever you call one of the generator’s methods. In this way, all function evaluation picks back up right after yield. You can see this in action by using multiple Python yield statements:

 

 When you use next(), Python calls .__next__() on the function you pass in as a parameter. There are some special effects that this parameterization allows, but it goes beyond the scope of this article. Experiment with changing the parameter you pass to next() and see what happens!

http://www.lryc.cn/news/413783.html

相关文章:

  • spring原理(自学第六天)
  • 案例分享—国外优秀ui设计作品赏析
  • 【C++】简约与清晰的编程艺术
  • java之WIFI信号模块
  • Mybatis面试
  • Centos 8系统xfs文件系统类型进行扩容缩容 (LVM)
  • C语言基础知识之函数指针和指针函数
  • 【Unity】web gl inputFied 中文输入,同时支持TextMeshInputFied,支持全屏
  • vue3+vite全局引入less变量和函数
  • H81002S 1.7mm网络变压器:BMS汽车蓝牙接收器中的超薄共模电感科技
  • C语言.回调函数
  • 《从零开始:使用Python构建简单Web爬虫》
  • 最新个人免签约支付系统源码|PHP源码 | 码支付系统 | ThinkPHP6框架 | 开源
  • The Llama 3 Herd of Models 第4部分后训练的全文
  • MongoDB性能调优
  • 【Qt开发】调试log日志QDebug重定向输出到textEdit等控件(qInstallMessageHandler回调函数)
  • 【JavaEE精炼宝库】 网络编程套接字——UDP业务逻辑 | TCP流套接字编程及业务逻辑实现
  • 前端过渡动画
  • actual combat 38 ——vue
  • 测试面试宝典(四十七)— 功能测试用例一般包含哪些内容
  • rust_mac环境安装
  • 【前端面试】七、算法-递归
  • CmsEasy逻辑漏洞--零元购
  • Linux 内核源码分析---I/O 体系结构与访问设备
  • 在cPanelWHM中如何重置 MySQL 用户帐户密码
  • 软件测试基础1--功能测试
  • 《计算机网络》(第8版)第9章 无线网络和移动网络 复习笔记
  • 非负数、0和正整数 限制最大值且保留两位小数在elementpuls表单中正则验证
  • Java多线程-----定时器(Timer)及其实现
  • 【Linux修行路】进度条小程序