On 21 April 2018 at 17:11, Steven D'Aprano <st...@pearwood.info> wrote:
> So can you explain specifically what odd function-scope behaviour you
> are referring to? Give an example please?

Once we allow name binding as an expression, there are three main
cases to consider in comprehensions:

1. Name binding in the result expression
2. Name binding a filter expression
3. Name binding in an iterable expression

The first two cases are fine (they happen in the implicit nested
scope, and hence don't affect the scope containing the comprehension),
but the behaviour in the third case bothered people, because it broke
down into two distinct subcases:

3a. For the outermost iterable, the binding always happens in the
surrounding scope, and hence will not be accessible from the rest of
the comprehension when used at class scope.
3b. For any nested iterables, the binding happens in the implicit
nested scope, as for other comprehension subexpressions

In the original version of PEP 572 (the one with sublocal scopes), the
consequences of 3a were just that you couldn't meaningfully use
assignment expressions in the outermost iterable expression of a
comprehension, since neither the implicitly nested scope nor the
surrounding scope would be able to see them. Too bad, so sad, don't do
that then (since it's pointless).

In the revised version of PEP 572 that just used regular local
assignment, the side effects of 3a were more concerning, since they
meant that we'd be bringing back the comprehension variable leakage
problem, albeit in a far more esoteric form. Even less defensibly, the
construct would just work at function scope, work, but define an
unexpected module attribute at module scope, and simply not work at
all at class scope. Hence the changes to the PEP to move even the
evaluation of the outermost iterable inside the implicitly nested
scope, rather than leaving it outside the way it is now.

Cheers,
Nick.

-- 
Nick Coghlan   |   ncogh...@gmail.com   |   Brisbane, Australia
_______________________________________________
Python-Dev mailing list
Python-Dev@python.org
https://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com

Reply via email to