<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On 10 May 2018 at 13:33, Guido van Rossum <span dir="ltr"><<a href="mailto:guido@python.org" target="_blank">guido@python.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div><div><div>(I vaguely recall this has been brought up before, but I'm too lazy to find the subtread. So it goes.)<br></div><div><br>PEP 572 currently seems to specify that when used in expressions, the precedence of `:=` is lower (i.e. it binds more tightly) than all operators except for the comma. I derive this from the single example `stuff = [[y := f(x), x/y] for x in range(5)]`.<br><br></div>From this it would follow that `f(a := 1, a)` is equivalent to `a = 1; f(1, 1)`, and also that `(a := 1, a)` is equivalent to `a = 1; (1, 1)`. (Although M.A.L. objected to this.)<br><br></div>But what should `a := 1, 1` at the top level (as a statement) do? On the one hand, analogy with the above suggest that it is equivalent to `a = 1; (1, 1)`. But on the other hand, it would be really strange if the following two lines had different meanings:<br><br></div><div>    a = 1, 1   # a = (1, 1)<br></div><div>    a := 1, 1  # a = 1; (1, 1)<br clear="all"></div><div><div><div><div><br></div><div>I now think that the best way out is to rule `:=` in the top level expression of an expression statement completely (it would still be okay inside parentheses, where it would bind tighter than comma).<br></div></div></div></div></div></blockquote><div><br></div><div>FWIW, this is one of the ambiguities that the generalised postfix expression form of the given clause would reduce fairly significantly by separating the result expression from the bound expression:<br><br></div><div>    a = 1, 1<br></div><div>    a given a = 1, 1 # As above, but also returns a<br></div><div>    a = 1, x, 3 given x = 2<br></div><div> <br></div><div>They also compose fairly nicely as an essentially right associative operator:<br><br><div>    a given a = 1, x, 3 given x = 2<br>    a given a = (1, x, 3 given x = 2) # Same as previous<br>    (a given a = 1), x, 3 given x = 2 # Forcing left-associativity<br><br>While you do have to repeat the bound name at least once, you gain a much clearer order of execution (while it's an order of execution that's right-to-left rather than left-to-right, "rightmost expression first" is the normal way assignments get evaluated anyway).<br></div><div></div><br></div>Cheers,<br></div><div class="gmail_quote">Nick.<br></div><br>-- <br><div class="gmail_signature" data-smartmail="gmail_signature">Nick Coghlan   |   <a href="mailto:ncoghlan@gmail.com" target="_blank">ncoghlan@gmail.com</a>   |   Brisbane, Australia</div>
</div></div>