python中的yield

python中的yield

http://qiaoqinqie.iteye.com/blog/988412


最近在学习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

 

查到伪代码如下:

  1. def close(self):   
  2.     try:   
  3.         self.throw(GeneratorExit)   
  4.     except (GeneratorExit, StopIteration):   
  5.         pass   
  6.     else:   
  7.         raise RuntimeError("generator ignored GeneratorExit")   
  8. # Other exceptions are not caught  
def close(self): 
    try: 
        self.throw(GeneratorExit) 
    except (GeneratorExit, StopIteration): 
        pass 
    else: 
        raise RuntimeError("generator ignored GeneratorExit") 
# Other exceptions are not caught

 最后是我写的一个小例子。对这个程序的理解,

花了一个晚上。主要是之前把send弄错了。以为是从yield之后执行。一定要小心这边的赋值

Python代码   收藏代码
  1. #! /usr/bin/python  
  2. # -*- coding:utf-8 -*-  
  3. ''''' 
  4. Created on 2011-4-6 
  5.  
  6. @author: ezioruan 
  7. '''  
  8.   
  9. def yield_test():  
  10.     first_value = yield 1  
  11.     if first_value:  
  12.         print 'first send is:' + first_value  
  13.     second_value = yield 2  
  14.     if second_value:  
  15.         print 'second send is:' + second_value  
  16.     third_value = yield 3  
  17.     if third_value:  
  18.         print 'third send is:' + third_value  
  19.       
  20.   
  21.   
  22. if __name__ == '__main__':  
  23.     print  '*'*30 + 'test next' +  '*'*30  
  24.     test_next = yield_test()  
  25.     print test_next.next()  
  26.     print test_next.next()  
  27.     print test_next.next()  
  28.     print  '*'*30 + 'test send' +  '*'*30  
  29.     test_send = yield_test()  
  30.     print test_send.next()  
  31.     print test_send.next()  
  32.     #send a value, that will continue from last yield  
  33.     print test_send.send('first')  
  34.     print  '*'*30 + 'test throw' +  '*'*30  
  35.     test_throw = yield_test()  
  36.     print test_throw.next()  
  37.     #That will end all the generator  
  38.     try:  
  39.         test_throw.throw(GeneratorExit)  
  40.     except GeneratorExit:  
  41.         print 'GeneratorExit'  
  42.     #call next or  send  again will stop  
  43.     try:  
  44.         test_throw.next()  
  45.     except StopIteration:  
  46.         print 'next StopIteration'  
  47.     try:  
  48.         test_throw.send('value')  
  49.     except StopIteration:  
  50.         print 'send StopIteration'  
  51.     print  '*'*30 + 'test close' +  '*'*30  
  52.     close_test = yield_test()  
  53.     print close_test.next()  
  54.     close_test.close()  
  55.      #call next or  send  again will stop  
  56.     try:  
  57.         test_throw.next()  
  58.     except StopIteration:  
  59.         print 'next StopIteration'  
  60.     try:  
  61.         test_throw.send('value')  
  62.     except StopIteration:  
  63.         print 'send StopIteration'  
  64.           
  65.       
  66.       
  67.       
  68.       
  69.       
  70.       
#! /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'
        
    
    
    
    
    
    

 输出

Python代码   收藏代码
  1. ******************************test next******************************  
  2. 1  
  3. 2  
  4. 3  
  5. ******************************test send******************************  
  6. 1  
  7. 2  
  8. second send is:first  
  9. 3  
  10. ******************************test throw******************************  
  11. 1  
  12. GeneratorExit  
  13. next StopIteration  
  14. send StopIteration  
  15. ******************************test close******************************  
  16. 1  
  17. next StopIteration  
  18. send StopIteration 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值