码迷,mamicode.com
首页 > 编程语言 > 详细

Python PEP342

时间:2017-04-05 00:52:04      阅读:276      评论:0      收藏:0      [点我收藏+]

标签:blog   stop   spec   www   tps   lock   print   other   thrown   

Specification Summary

    1. Redefine "yield" to be an expression, rather than a statement.
       The current yield statement would become a yield expression
       whose value is thrown away.  A yield expression‘s value is
       None whenever the generator is resumed by a normal next() call.

除了作为一个statement将一个函数变成一个generator以外,yield可以被视为是一个表达式,并用于赋值。

比如

>>> def whizbang():
        for i in range(10):
            x = yield i
            print got sent:, x


>>> i = whizbang()
>>> next(i)
0
>>> next(i)
got sent: None
1
>>> i.send("hi")
got sent: hi
2
    2. Add a new send() method for generator-iterators, which resumes
       the generator and "sends" a value that becomes the result of the
       current yield-expression.  The send() method returns the next
       value yielded by the generator, or raises StopIteration if the
       generator exits without yielding another value.
见1,一个yield expression如果不通过send传入一个值,那么其值就默认是None


其余见https://www.python.org/dev/peps/pep-0342/

Python PEP342

标签:blog   stop   spec   www   tps   lock   print   other   thrown   

原文地址:http://www.cnblogs.com/autoria/p/6666991.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!