Python PEP342

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python PEP342相关的知识,希望对你有一定的参考价值。

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的主要内容,如果未能解决你的问题,请参考以下文章

python yield generator 详解

002.[python学习]python编码规范pep8学习——PEP8第一部分代码布局

python代码风格指南:pep8 中文版

Python代码规范(PEP8)问题及解决

Python 编写代码 检查是否遵循PEP 8标准

Pycharm配置autopep8教程,让Python代码更符合pep8规范