最近在学习django。在看到里面的源码的时候,发现有很多地方使用了yield,不知是干什么的,于是就上网查了一下。
呵呵,python的这个yield和java的差别真的是太大了。
yield在python2.5以后是一个生成器。也就是说 。如果一个函数里面有yield。那么他就可以通过一下四个方法进行操作
(1):next 执行到一个yield。然后程序返回。并且挂起。此时所有的状态被保留。如全局变量
(2):send(msg) 从上次挂起的yield那边开始执行。并且把msg赋值给yield的表达式。例如上一次是在 m = yield 5断掉。那么send(10),m将被赋值成10.然后继续执行,直到执行到下一个yield。然后依然挂起。所以不能在第一次调用的时候,就send,因为这个时候,根本就还没有执行到任何的yield。
(3)throw(type[, value[, traceback]]),可以抛出指定异常终止程序
(4):close(). 其实就是执行throws(GeneratorExit)
查到伪代码如下:
def close(self):
try:
self.throw(GeneratorExit)
except (GeneratorExit, StopIteration):
pass
else:
raise RuntimeError("generator ignored GeneratorExit")
# Other exceptions are not caught
最后是我写的一个小例子。对这个程序的理解,
花了一个晚上。主要是之前把send弄错了。以为是从yield之后执行。一定要小心这边的赋值
#! /usr/bin/python
# -*- coding:utf-8 -*-
'''
Created on 2011-4-6
@author: ezioruan
'''
def yield_test():
first_value = yield 1
if first_value:
print 'first send is:' + first_value
second_value = yield 2
if second_value:
print 'second send is:' + second_value
third_value = yield 3
if third_value:
print 'third send is:' + third_value
if __name__ == '__main__':
print '*'*30 + 'test next' + '*'*30
test_next = yield_test()
print test_next.next()
print test_next.next()
print test_next.next()
print '*'*30 + 'test send' + '*'*30
test_send = yield_test()
print test_send.next()
print test_send.next()
#send a value, that will continue from last yield
print test_send.send('first')
print '*'*30 + 'test throw' + '*'*30
test_throw = yield_test()
print test_throw.next()
#That will end all the generator
try:
test_throw.throw(GeneratorExit)
except GeneratorExit:
print 'GeneratorExit'
#call next or send again will stop
try:
test_throw.next()
except StopIteration:
print 'next StopIteration'
try:
test_throw.send('value')
except StopIteration:
print 'send StopIteration'
print '*'*30 + 'test close' + '*'*30
close_test = yield_test()
print close_test.next()
close_test.close()
#call next or send again will stop
try:
test_throw.next()
except StopIteration:
print 'next StopIteration'
try:
test_throw.send('value')
except StopIteration:
print 'send StopIteration'
输出
******************************test next******************************
1
2
3
******************************test send******************************
1
2
second send is:first
3
******************************test throw******************************
1
GeneratorExit
next StopIteration
send StopIteration
******************************test close******************************
1
next StopIteration
send StopIteration
分享到:
相关推荐
python中yield的用法全文共3页,当前为第1页。python中yield的用法全文共3页,当前为第1页。python中yield的用法 python中yield的用法全文共3页,当前为第1页。 python中yield的用法全文共3页,当前为第1页。 Python...
Python中yield返回生成器的详细方法
主要介绍了python中yield的用法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
主要介绍了通过实例简单了解Python中yield的作用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
1.程序开始执行以后,因为foo函数中有yield关键字,所以foo函数并不会真的执行,而是先得到一个生成器g(相当于一个对象) 2.直到调用next方法,fo
带有yield的函数在Python中被称之为generator(生成器),也就是说,当你调用这个函数的时候,函数内部的代码并不立即执行 ,这个函数只是返回一个生成器(Generator Iterator)。 def generator(): for i in range(10...
为了理解yield是什么,首先要明白生成器(generator)是什么,在讲生成器之前先说说迭代器(iterator),当创建一个列表(list)时,你可以逐个的读取每一项,这就叫做迭代(iteration)。 >>> mylist = [1, 2, 3] >>> ...
接触python,yield就有点难度啦、都知道包含这个yield的函数就不是普通函数啦。就是一个生成器函数。 类型: 白话,他跟return的区别就是。他会为用户保留一个断点。return,程序执行一次之后,就会一切重新开始。...
主要介绍了Python中的yield函数,yield函数是生成器中的一个常用函数,本文来自于IBM官方网站的开发者文档的翻译,需要的朋友可以参考下
主要介绍了举例详解Python中yield生成器的用法,包括其在多线程multiprocess下的使用示例,非常推荐!需要的朋友可以参考下
python方法 python_yield用法详解
今天在看其他同事的代码时,发现一个没使用过的python关键字 :yield 先问了一下同事,听他说了几句,有个模糊的印象,仅仅是模糊而已。于是自己去搜搜资料看。看了半天,逐渐清晰了。不过在工作机制以及应用上...
首先,把yield理解成return,可以用作返回数据,如下: def f(): yield 9 next(f()) #结果为 9 如果你用一个变量接受该值,首次执行时并不会执行函数,而是先得到一个生成器,如下: def f(): print(提示字符!) ...
前言 最近在捣鼓Autobahn,它有给出个例子是基于asyncio 的,想着说放到pypy3上跑跑看竟然就……失败了...好吧这个标题是我google出来的,yield from的前世今生都在 这个PEP里面,总之大意是原本的yield语句只能将CPU控