收益 - 陈述或表达? [英] yield - statement or expression?

查看:191
本文介绍了收益 - 陈述或表达?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我一直在阅读这个,并发现了发送值到生成器。

So, I've been reading this, and found out about sending values to generator.

现在我有点困惑。产生一个语句或一个表达式?它不使用括号语法,如函数,因此它看起来像语句。但是它返回值,所以就像表达式。

And now I'm kinda confused. Is yield a statement or an expression? It doesn't use parenthesis syntax, like functions, so it looks like statement. But it returns value, so it's like expression.

不久之前,我曾经谈过为什么python没有'如果x = foo() :(...)'? (为什么我们不能分配if语句条件)。我说,那个语句是原子的,所以赋值语句和if语句应该分开。现在,我不知道该怎么想了。

Not so long ago I've had this conversation about "Why python doesn't have 'if x=foo(): (...)'?" (why can't we assign in if statement condition). I said, that statements are atomic, so assignment statement and if statement should be separated. Now, I don't know what to think anymore.

==编辑==

我做了我的阅读的公平份额。

I did my fair share of reading.

http://docs.python.org/2/reference/expressions.html#generator.send - value参数成为当前的yield表达式的结果。

http://docs.python.org/2/reference/expressions.html#generator.send - "The value argument becomes the result of the current yield expression."

所以,产生HAS值。我得到它,所有的文档都说这是一个声明,但如果语句可能有价值,那么表达式和语句之间的区别是什么?

So, yield HAS value. I get it, that all docs say it's a statement, but if statement may have value, then what the hell is the difference between expression and statement?

另外,我知道什么是生成器,协同程序等等。我需要元语义或文本语义:D

Also, I know what are generators, coroutines, etc, etc. I need meta-semantics, or semantics for docs :D

==最终答案==

显然,收益率可以是两者。只需阅读这些答案:(1)(2) - 我发现他们最有帮助。

Apparently, yield can be both. Just read those answers: (1) and (2) - I found them most helpful.

推荐答案

yield 是一个表达式。它曾经是一个声明,它最常用作整个语句,但是在Python 2.5中,它被转换为一个表达式作为新的协同程序支持的一部分。它通常被称为收益声明,部分是由于过时的文档和知识,部分原因是它主要用作声明。您可以在 PEP 342 中阅读。

yield is an expression. It used to be a statement, and it's most commonly used as an entire statement, but in Python 2.5, it was turned into an expression as part of new coroutine support. It's still commonly referred to as the "yield statement", partly due to outdated documentation and knowledge and partly because it's mostly used as a statement anyway. You can read about that in PEP 342.

除了以下表单:

yield whatever
x = yield whatever

一个yield表达式必须在任何地方发生括号,以避免语法中的歧义。

a yield expression must be parenthesized wherever it occurs, to avoid ambiguity in the syntax.

这篇关于收益 - 陈述或表达?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆