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

前端 未结 1 1098
轻奢々
轻奢々 2021-01-18 00:36

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

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


        
1条回答
  •  臣服心动
    2021-01-18 01:08

    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 
    yield from 
    

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

    (yield )
    (yield from )
    

    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]
    :1: DeprecationWarning: 'yield' inside list comprehension
     at 0x000002E06BC1F1B0>
    

    0 讨论(0)
提交回复
热议问题