Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 隐式定义生成器中的产量?_Python 3.x_Generator_Yield - Fatal编程技术网

Python 3.x 隐式定义生成器中的产量?

Python 3.x 隐式定义生成器中的产量?,python-3.x,generator,yield,Python 3.x,Generator,Yield,它说,在中国 为了避免干扰生成器表达式本身的预期操作,隐式定义的生成器中禁止表达式的yield和yield 这意味着什么?这意味着你不能做以下事情: sum(yield x*x for x in range(10)) 这显然是因为明确的收益率与允许的总和(x*x代表范围(10)内的x)中的隐含收益率相互作用的方式将难以解释,一旦您开始使用高级技术,如generator.send和generator.throw这是指在生成器表达式中使用屈服表达式,例如: >>> g = ((y

它说,在中国

为了避免干扰生成器表达式本身的预期操作,隐式定义的生成器中禁止表达式的yield和yield


这意味着什么?

这意味着你不能做以下事情:

sum(yield x*x for x in range(10))

这显然是因为明确的
收益率
与允许的
总和(x*x代表范围(10)内的x)中的隐含收益率相互作用的方式
将难以解释,一旦您开始使用高级技术,如
generator.send
generator.throw

这是指在生成器表达式中使用屈服表达式,例如:

>>> g = ((yield x**2) for x in [1,2,3])                                                 
>>> list(g)                                                                             
[1, None, 4, None, 9, None]
或:

在Python 3.8+中,此语法现在是,将来也将是一个
SyntaxError
。有关更多详细信息,请参阅

>>> t = "hello", "world"                                                                
>>> g = ((yield from t) for x in 'xyz')                                                 
>>> list(g)                                                                             
['hello', 'world', None, 'hello', 'world', None, 'hello', 'world', None]