Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

yield - statement or expression?

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.

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.

== EDIT ==

I did my fair share of reading.

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

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?

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

== FINAL ANSWER ==

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

like image 900
Filip Malczak Avatar asked Nov 22 '13 10:11

Filip Malczak


People also ask

What does the yield statement do?

The yield statement suspends function's execution and sends a value back to the caller, but retains enough state to enable function to resume where it is left off. When resumed, the function continues execution immediately after the last yield run.

What is the difference between expression and statement in Python?

In short – An Expression always evaluates to a value. And, A statement does something, like creating a variable or displaying a value, it only does whatever the statement says.

What is the purpose of yield statement in PHP?

The yield keyword is used to create a generator function. Generator functions act as iterators which can be looped through with a foreach loop. The value given by the yield keyword is used as a value in one of the iterations of the loop.

What is yield in programming?

In computer science, yield is an action that occurs in a computer program during multithreading, of forcing a processor to relinquish control of the current running thread, and sending it to the end of the running queue, of the same scheduling priority.


1 Answers

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.

Aside from the following forms:

yield whatever
x = yield whatever

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

like image 72
user2357112 supports Monica Avatar answered Nov 15 '22 12:11

user2357112 supports Monica