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?
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.
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.
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.
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+)
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With