首页 > 编程语言 > 详细

Python PEP342

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

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

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!