On 27.06.2018 16:25, Greg Ewing wrote:
Ivan Pozdeev via Python-Dev wrote:
Using this assigned result elsewhere in the same expression (akin to regex backreferences) is not a part of the basic idea actually.
If that's true, then the proposal has mutated into something that has *no* overlap whatsoever with the use case that started this whole discussion,
I don't know what and where "started" it (AFAIK the idea has been around for years) but for me, the primary use case for an assignment expression is to be able to "catch" a value into a variable in places where I can't put an assignment statement in, like the infamous `if re.match() is not None'.
which was about binding a temporary variable in a comprehension, for use *within* the comprehension.
Then I can't understand all the current fuss about scoping. AFAICS, it's already like I described in https://mail.python.org/pipermail/python-dev/2018-June/154067.html : the outermost iterable is evaluated in the local scope while others in the internal one:
In : [(l,i) for l in list(locals())[:5] for i in locals()] Out: [('__name__', 'l'), ('__name__', '.0'), ('__builtin__', 'l'), ('__builtin__', '.0'), ('__builtin__', 'i'), ('__builtins__', 'l'), ('__builtins__', '.0'), ('__builtins__', 'i'), ('_ih', 'l'), ('_ih', '.0'), ('_ih', 'i'), ('_oh', 'l'), ('_oh', '.0'), ('_oh', 'i')]
(note that `i' is bound after the first evaluation of internal `locals()' btw, as to be expected)
If the "temporary variables" are for use inside the comprehension only, the assignment expression needs to bind in the current scope like the regular assignment statement, no changes are needed!
It depends on the evaluation order (and whether something is evaluated at all),
Which to my mind is yet another reason not to like ":=".