On 7 May 2018 at 12:51, Nick Coghlan <ncogh...@gmail.com> wrote:

> If any other form of comprehension level name binding does eventually get
> accepted, then inline scope declarations could similarly be used to hoist
> values out into the surrounding scope:
>
>         rem = None
>         while any((nonlocal rem := n % p) for nonlocal p in small_primes):
>             # p and rem were declared as nonlocal in the nested scope, so
> our rem and p point to the last bound value
>

Thinking about it a little further, I suspect the parser would reject
"nonlocal name := ..." as creating a parsing ambiguity at statement level
(where it would conflict with the regular nonlocal declaration statement).

The extra keyword in the given clause would avoid that ambiguity problem:

        p = rem = None
        while any(rem for nonlocal p in small_primes given nonlocal rem = n
% p):
            # p and rem were declared as nonlocal in the nested scope, so
our p and rem refer to their last bound values

Such a feature could also be used to make augmented assignments do
something useful at comprehension scope:

    input_tally = 0
    process_inputs(x for x in input_iter given nonlocal input_tally += x)

Cheers,
Nick.

-- 
Nick Coghlan   |   ncogh...@gmail.com   |   Brisbane, Australia
_______________________________________________
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to