[Python-ideas] A comprehension scope issue in PEP 572

Chris Angelico rosuav at gmail.com
Fri May 11 09:08:57 EDT 2018


On Fri, May 11, 2018 at 9:15 PM, Nick Coghlan <ncoghlan at gmail.com> wrote:
> * *maybe* discover that even the above expansion isn't quite accurate, and
> that the underlying semantic equivalent is actually this (one way to
> discover this by accident is to have a name error in the outermost iterable
> expression):
>
>     def _genexp(_outermost_iter):
>         for x in _outermost_iter:
>             yield x
>
>     _result = _genexp(_outermost_iter)
>
> * and then realise that the optimised list comprehension form is essentially
> this:
>
>     def _listcomp(_outermost_iter):
>         result = []
>         for x in _outermost_iter:
>             result.append(x)
>         return result
>
>     _result = _listcomp(data)
>
> Now that "yield" in comprehensions has been prohibited, you've learned all
> the edge cases at that point

Not quite! You missed one, just because comprehensions aren't weird
enough yet. AFAIK you can't tell with the list comp, but with the
genexp you can (by not iterating over it).

>     def _genexp(_outermost_iter):
>         for x in _outermost_iter:
>             yield x
>
>     _result = _genexp(data)

It's actually this:

     def _genexp(_outermost_iter):
         for x in _outermost_iter:
             yield x

     _result = _genexp(iter(_outermost_iter))

I don't think there's anything in the main documentation that actually
says this, although PEP 289 mentions it in the detaily bits. [1]

ChrisA

[1] https://www.python.org/dev/peps/pep-0289/#the-details


More information about the Python-ideas mailing list