generator yield超详解
来源:互联网 发布:橙光制作工具 mac 编辑:程序博客网 时间:2024/04/30 06:08
To understand what yield
does, you must understand what generators are. And before generators come iterables.
Iterables
When you create a list, you can read its items one by one. Reading its items one by one is called iteration:
>>> mylist = [1, 2, 3]>>> for i in mylist:... print(i)123
mylist
is an iterable. When you use a list comprehension, you create a list, and so an iterable:
>>> mylist = [x*x for x in range(3)]>>> for i in mylist:... print(i)014
Everything you can use "for... in...
" on is an iterable; lists
, strings
, files...
These iterables are handy because you can read them as much as you wish, but you store all the values in memory and this is not always what you want when you have a lot of values.
Generators
Generators are iterators, but you can only iterate over them once. It's because they do not store all the values in memory, they generate the values on the fly:
>>> mygenerator = (x*x for x in range(3))>>> for i in mygenerator:... print(i)014
It is just the same except you used ()
instead of []
. BUT, you cannot perform for i in mygenerator
a second time since generators can only be used once: they calculate 0, then forget about it and calculate 1, and end calculating 4, one by one.
Yield
yield
is a keyword that is used like return
, except the function will return a generator.
>>> def createGenerator():... mylist = range(3)... for i in mylist:... yield i*i...>>> mygenerator = createGenerator() # create a generator>>> print(mygenerator) # mygenerator is an object!<generator object createGenerator at 0xb7555c34>>>> for i in mygenerator:... print(i)014
Here it's a useless example, but it's handy when you know your function will return a huge set of values that you will only need to read once.
To master yield
, you must understand that when you call the function, the code you have written in the function body does not run. The function only returns the generator object, this is a bit tricky :-)
Then, your code will be run each time the for
uses the generator.
Now the hard part:
The first time the for
calls the generator object created from your function, it will run the code in your function from the beginning until it hits yield
, then it'll return the first value of the loop. Then, each other call will run the loop you have written in the function one more time, and return the next value, until there is no value to return.
The generator is considered empty once the function runs but does not hit yield
anymore. It can be because the loop had come to an end, or because you do not satisfy an "if/else"
anymore.
原文:https://stackoverflow.com/questions/231767/what-does-the-yield-keyword-do
- generator yield超详解
- python yield generator 详解
- python yield generator 详解
- python yield generator 详解
- python yield generator 详解
- Python Yield Generator 详解
- yield / generator
- Python生成器详解(从Iterable,Iterator知Generator,Yield)
- python generator / yield statement
- Python yield expression (generator)
- python中yield generator
- generator and yield
- Generator and yield
- python iterator generator yield
- Python yield and generator
- python yield和generator
- yield,generator type
- python yield generator
- plsqldev32位链接ORACL64位数据库 免客户端安装
- Oracle安装与卸载
- poj2478 Farey Sequence
- leetcode[Heaters]
- HDU6070 2017杭电多校联赛第四场-Dirt Ratio
- generator yield超详解
- DFS和BFS的思路模板伪代码
- POJ 1026 Cipher(置换)
- 暑期培训心得
- 简单任务调度问题(贪心)
- Android4.4 Uri获取图片及document理解
- iOS用域名或主机名获取IP地址(包含C语言接口 和iOS原生)
- API网关orange在ubuntu安装记录
- HDU 2094 产生冠军