Python yield解析
Pyhton generators and the yield keyword
At a glance,the yield statement is used to define generators,repalcing the return of a function to provide a result to its caller without destorying local variables.Unlike a function,where on each call it starts with new set of variables,a generator will resume the execution where it was left off.
被yield修饰的函数,会被python解释器当作generator
示例代码:
def countdown():
i=5
while i>0:
yield i
i-=1
for i in countdown():
print(i)
print('********************')
def gen(n):#斐波那契数列
i=0
pref=1#s[n-2]
pres=2#s[n-1]
cur=0#s[n]
while i<n:
if i<2:
yield 1
else:
cur=pref+pres
yield cur
pref=pres
pres=cur
i=i+1
if __name__=='__main__':
for item in gen(20):
print(item)
输出:
5
4
3
2
1
********************
1
1
2
3
5
8
13
21
34
55
89
144
233
377
610
987
1597
2584
4181
6765
调用gen函数不会执行gen函数,它返回一个迭代器对象,第二次迭代时从yield x的下一条语句开始执行,再到下一次遇到yield。
虽然执行流程按照函数的流程执行,但是每次执行到yield时就会中断,它使一个函数获得了迭代能力。
Python yield解析的更多相关文章
- python 关键字yield解析
python 关键字yield解析 yield 的作用就是把一个函数变成一个 generator,带有 yield 的函数不再是一个普通函数,Python 解释器会将其视为一个 generator.y ...
- Python中yield解析
小探yield 查看 python yield 文档 yield expressions: Using a yield expression in a function's body causes t ...
- Python 文本解析器
Python 文本解析器 一.课程介绍 本课程讲解一个使用 Python 来解析纯文本生成一个 HTML 页面的小程序. 二.相关技术 Python:一种面向对象.解释型计算机程序设计语言,用它可以做 ...
- 用 ElementTree 在 Python 中解析 XML
用 ElementTree 在 Python 中解析 XML 原文: http://eli.thegreenplace.net/2012/03/15/processing-xml-in-python- ...
- Python yield与实现
Python yield与实现 yield的功能类似于return,但是不同之处在于它返回的是生成器. 生成器 生成器是通过一个或多个yield表达式构成的函数,每一个生成器都是一个迭代器(但是迭 ...
- Python XML解析(转载)
Python XML解析 什么是XML? XML 指可扩展标记语言(eXtensible Markup Language). 你可以通过本站学习XML教程 XML 被设计用来传输和存储数据. XML是 ...
- 【转】Python yield 使用浅析
转载地址: www.ibm.com/developerworks/cn/opensource/os-cn-python-yield/ Python yield 使用浅析 初学 Python 的开发者经 ...
- python高效解析日志入库
python脚本解析日志文件入库一般有三个重要的步骤:读文件.解析文件.入库.在这三个方面下功夫,可确保我们获得最优的性能(这里不讨论并发) 1 读文件:一次读一行,磁盘IO太多,效率低下:一次性读如 ...
- Python yield 使用浅析(转)
Python yield 使用浅析 初学 Python 的开发者经常会发现很多 Python 函数中用到了 yield 关键字,然而,带有 yield 的函数执行流程却和普通函数不一样,yield 到 ...
随机推荐
- awk 新手入门笔记
转自:http://www.habadog.com/2011/05/22/awk-freshman-handbook/ awk新手入门笔记 @作者 : habadog@邮箱 : habadog1203 ...
- swift--调用系统单例实现打电话
//自动打开拨号页面并自动拨打电话 var phone="15974462468"; UIApplication.sharedApplication().openURL(NSURL ...
- Python MongoDB 教程
基于菜鸟教程实际操作后总结而来 Python MongoDB MongoDB 是目前最流行的 NoSQL 数据库之一,使用的数据类型 BSON(类似 JSON). MongoDB 数据库安装与介绍可以 ...
- SprinMVC接收参数乱码解决篇
1.Spring 默认的字符编码格式为iso-8859-1,为此Spring专门提供了字符过滤器org.springframework.web.filter.CharacterEncodingFilt ...
- file.seek()/tell()-笔记
---------------------------------------------------------------------------------------------------- ...
- LeetCode 712. Minimum ASCII Delete Sum for Two Strings
Given two strings s1, s2, find the lowest ASCII sum of deleted characters to make two strings equal. ...
- SQLServer中的Cross Apply、Outer Apply
https://www.2cto.com/database/201304/206330.html
- js两个整数之间求和
const GetSum = (a, b) => { let min = Math.min(a, b), max = Math.max(a, b); return (max - min + 1) ...
- ZooKeeper可视化Web管理工具收集(待实践)
原来ZooKeeper是有Web管理后台的.但是仅限于操作ZooKeeper的数据,如果要监控性能,估计要借助Nagios去配合. 这些工具应该ZK UI最好用,下面是收集的一些工具安装教程: htt ...
- Android 65K问题之Multidex原理分析及NoClassDefFoundError的解决方法
Android 65K问题相信困惑了不少人,尽管AS的出来能够通过分dex高速解决65K问题,可是同一时候也easy由于某些代码没有打包到MainDex里引起NoClassDefFoundError. ...