[Python-Dev] PEP 572: Assignment Expressions

Nick Coghlan ncoghlan at gmail.com
Sun Apr 22 01:48:05 EDT 2018


On 22 April 2018 at 03:41, Steven D'Aprano <steve at pearwood.info> wrote:
> We *already* have the rule that the outermost iterable is special,
> except it isn't a rule precisely, since (as far as I know) it isn't
> documented anywhere, nor was it ever planned as a feature.

It's a deliberate feature of generator expressions:
https://www.python.org/dev/peps/pep-0289/#the-details

Covered in the language reference here:
https://docs.python.org/3/reference/expressions.html#generator-expressions

It's then inherited by comprehensions through the intentional semantic
equivalences between:

    [x for x in iterable] <-> list(x for x in iterable)
    {x for x in iterable} <-> set(x for x in iterable)
    {k(x):v(x) for x in iterable} <-> set((k(x), v(x) for x in iterable))

The consequences of those equivalences weren't historically spelled
out in the language reference, but we fixed that omission when
deprecating the use of "yield" & "yield from" in comprehensions for
3.7: https://github.com/python/cpython/commit/73a7e9b10b2ec9636e3c6396cf7b3695f8ed1856
(we had to in order to explain why you *can* still use "yield" and
"yield from" in the outermost iterable - it's because the restriction
only applies to the implicitly nested scope).

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia


More information about the Python-Dev mailing list