python中的yield

最近在学习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
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值