On Mon, Jun 25, 2018 at 4:06 AM, Steven D'Aprano email@example.com 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*.