Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between expression and statement using Python yield [duplicate]

Tags:

python

Possible Duplicate:
Expression Versus Statement

What's the difference between expression and statement in Python?

I have never thought of this question until I am learning Python generator that said "use yield as an expression"

Also, can you explain this question in the context of Python generator that 'use yield as an expression'?

like image 827
ming.kernel Avatar asked Dec 27 '22 17:12

ming.kernel


1 Answers

An expression can be evaluated to return a value. Any expression can also be used as a statement.

To put it another way, if you can write a = ..., then ... is an expression. So 2*3 and zip(x,y) are expressions.

Something like raise Exception is a statement but not an expression: you can't write a = (raise Exception).

yield being an expression means that b = (yield a) is valid code in a generator. If you use the generator's send() method, b is set to the value you pass in.

like image 53
Thomas K Avatar answered Dec 29 '22 07:12

Thomas K