On Mon, Jun 25, 2018 at 4:06 AM, Steven D'Aprano <firstname.lastname@example.org> wrote:
> Remember, the driving use-case which started this (ever-so-long)
> discussion was the ability to push data into a comprehension and then
> update it on each iteration, something like this:
> x = initial_value()
> results = [x := transform(x, i) for i in sequence]
Which means there is another option.
5. Have the assignment be local to the comprehension, but the initial
value of ANY variable is looked up from the surrounding scopes.
That is: you will NEVER get UnboundLocalError from a
comprehension/genexp; instead, you will look up the name as if it were
in the surrounding scope, either getting a value or bombing with
regular old NameError.
Or possibly variations on this such as "the immediately surrounding
scope only", rather than full name lookups. It'd have an awkward
boundary somewhere, whichever way you do it.
This isn't able to send information *out* of a comprehension, but it
is able to send information *in*.
But this "horrifies" me for a slightly different reason: it effectively introduces a new case of dynamic scoping, which Python used to do everywhere but has long switched away from, with the exception of class scopes (whose difference with function scopes sometimes confuses people -- usually people who put too much code in their class scope).