Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

yield(x) vs. (yield(x)): parentheses around yield in python

Using Python 3.4, I get SyntaxError: invalid syntax here:

>>> xlist = [1,2,3,4,5]
>>> [yield(x) for x in xlist]
SyntaxError: invalid syntax

But this generates a generator object:

>>> [(yield(x)) for x in xlist]
<generator object <listcomp> at 0x00000076CC8E5DB0>

Are round brackets around yield required?

like image 823
GoGo Avatar asked Jun 15 '16 20:06

GoGo


People also ask

How do you yield multiple variables in Python?

If you want to return multiple values from a function, you can use generator functions with yield keywords. The yield expressions return multiple values. They return one value, then wait, save the local state, and resume again.

What is difference between yield and return in Python?

The yield statement hauls the function and returns back the value to the function caller and restart from where it is left off. The yield statement can be called multiple times. While the return statement ends the execution of the function and returns the value back to the caller.

Is yield faster Python?

permutations. Using a generator (ie yield ) reduces this by approx. 20 % Using a generator and generating tuples is the fastest, about twice the time of itertools.

What does yield from mean in Python?

In applied usage for the Asynchronous IO coroutine, yield from has a similar behavior as await in a coroutine function. Both of which is used to suspend the execution of coroutine. yield from is used by the generator-based coroutine. await is used for async def coroutine. ( since Python 3.5+)


1 Answers

The yield keyword can be used in two ways: As a statement, and as an expression.

The most common use is as a statement within generator functions, usually on its own line and all. It can be used like this:

yield <expr>
yield from <expr>

The yield expression however can be used wherever expressions are allowed. However, they require a special syntax:

(yield <expr>)
(yield from <expr>)

As you can see, the parentheses are part of the syntax to make yield work as an expression. So it’s syntactically not allowed to use the yield keyword as an expression without parentheses. That’s why you need to use parentheses in the list comprehension.

That being said, if you want to use list comprehension syntax to create a generator, you should use the generator expression syntax instead:

(x for x in xlist)

Note the parentheses instead of the square brackets to turn this from a list comprehension into a generator expression.


Note that starting with Python 3.7, yield expressions are deprecated within comprehensions and generator expressions (apart from within the iterable of the left-most for clause), to ensure that comprehensions are properly evaluated. Starting with Python 3.8, this will then cause a syntax error.

This makes the exact list comprehension in the question a deprecated usage:

>>> [(yield(x)) for x in xlist]
<stdin>:1: DeprecationWarning: 'yield' inside list comprehension
<generator object <listcomp> at 0x000002E06BC1F1B0>
like image 66
poke Avatar answered Nov 15 '22 15:11

poke