On Sun, Jun 24, 2018 at 09:24:39AM -0700, Guido van Rossum wrote:
A quick follow-up: PEP 572 currently has two ideas: (a) introduce := for inline assignment, (b) when := is used in a comprehension, set the scope for the target as if the assignment occurred outside any comprehensions. It seems we have more support for (a) than for (b) -- at least Nick and Greg seem to be +0 or better for (a) but -1 for (b). IIRC (b) originated with Tim.
I'm not sure who came up with the idea first, but as I remember it, the first mention of this came in a separate thread on Python-Ideas:
so possibly I'm to blame :-)
That thread starts here:
If I did get the idea from Tim, I don't remember doing so.
But his essay on the topic, included as Appendix A ( https://www.python.org/dev/peps/pep-0572/#appendix-a-tim-peters-s-findings) does not even mention comprehensions. However, he did post his motivation for (b) on python-ideas, IIRC a bit before PyCon; and the main text of the PEP gives a strong motivation ( https://www.python.org/dev/peps/pep-0572/#scope-of-the-target). Nevertheless, maybe we should compromise and drop (b)?
I will have more to say about the whole "comprehensions are their own scope" issue later. But I'd like to see Nick's proposed PEP, or at least a draft of it, before making any final decisions.
If it came down to it, I'd be happy with the ability to declare an assignment target nonlocal in the comprehension if that's what it takes. What do you think of this syntax?
[global|nonlocal] simple_target := expression
Inside a comprehension, without a declaration, the target would be sublocal (comprehension scope); that should make Nick happier :-)