zoukankan      html  css  js  c++  java
  • Python语法

     
    yield是个表达式而不仅仅是个语句,所以可以使用x = yield r 这样的语法, yield表达式可以接收send()发出的参数,yield表达式是跟send方法一起配合使用
     
    send方法
    next()和send()在一定意义上作用是相似的,区别是send()可以传递yield表达式的值进去,而next()不能传递特定的值,只能传递None进去。因此,我们可以看做c.next() 和 c.send(None) 作用是一样的。

    def gen():

        value=0

        while True:

            receive = yield value

            if receive=='e':

                break

            value = 'got: %s' % receive

    g=gen()

    print(g.send(None))    

    print(g.send('aaa'))

    print(g.send(3))

    print(g.send('e'))

    执行流程:

    1. 通过g.send(None)或者next(g)可以启动生成器函数,并执行到第一个yield语句结束的位置。此时,执行完了yield语句,但是没有给receive赋值。yield value会输出初始值0注意:在启动生成器函数时只能send(None),如果试图输入其它的值都会得到错误提示信息。
    2. 通过g.send(‘aaa’),会传入aaa,并赋值给receive,然后计算出value的值,并回到while头部,执行yield value语句有停止。此时yield value会输出”got: aaa”,然后挂起。
    3. 通过g.send(3),会重复第2步,最后输出结果为”got: 3″
    4. 当我们g.send(‘e’)时,程序会执行break然后推出循环,最后整个函数执行完毕,所以会得到StopIteration异常。
    执行结果:

    0

    got: aaa

    got: 3

    Traceback (most recent call last):

      File "C:string.bak.py", line 20, in <module>

        print(g.send('e'))

    StopIteration

    yield表达式实现协程

    def consumer():
        r = 'yield'
        while True:
            #当下边语句执行时,先执行yield r,然后consumer暂停,此时赋值运算还未进行
            #等到producer调用send()时,send()的参数作为yield r表达式的值赋给等号左边
            n = yield r #yield表达式可以接收send()发出的参数
            if not n:
                return
            print('[CONSUMER] Consuming %s...' % n)
            r = '200 OK'
    
    def produce(c):
        c.send(None) #send需要先调用send(None),因为只有生成器是暂停状态才可以接收send的参数
        n = 0
        while n < 5:
            n = n + 1
            print('[PRODUCER] Producing %s...' % n)
            r = c.send(n)   #调用consumer生成器
            print('[PRODUCER] Consumer return: %s' % r)
        c.close()
    
    c = consumer()
    produce(c)
     
    理解send()恢复生成器的过程
    def gen():
        a = yield 1
        print('yield a % s' % a)
        b = yield 2
        print('yield b % s' % b)
        c = yield 3
        print('yield c % s' % c)
    
    
    r = gen()
    x = next(r)
    print('next x %s' % x)
    y = r.send(10)
    print('next y %s' %y)
    z = next(r)
    print('next z %s' % z)
    

    可以看到实际上y=r.send(10) 的参数10是赋给了a。整个运行过程即执行x=next(r) 之后,gen()执行了yield 1 然后暂停,没有进行对a的赋值。但再调用y=r.send(10) 时赋值过程继续,并把10赋给了a.

     
  • 相关阅读:
    XML炸弹
    IP分片攻击——就是发送部分分片报文,让对方一直等待从而耗对方内存的DoS攻击
    灰色软件——广告软件,拨号软件,远程访问软件等
    rootkit——一种特殊的恶意软件,它的功能是在安装目标上隐藏自身及指定的文件、进程和网络链接等信息,一般都和木马、后门等其他恶意程序结合使用
    漏洞利用 Exploit---利用默认口令、IP假冒、应用漏洞
    SequenceFile文件
    随机森林和GBDT的几个核心问题
    机器学习中的算法(1)-决策树模型组合之随机森林与GBDT
    直方图中最大矩形面积
    openMP多线程编程
  • 原文地址:https://www.cnblogs.com/sysnap/p/6590965.html
Copyright © 2011-2022 走看看