On 03/07/2018 06:47, Tim Peters wrote:
It's late to raise this,
By months, yes ;-)
but what exactly are the objections to the syntax expr -> name # or variations such as expr => name instead of name := expr
The PEP mentions that this syntax does not have a problem that "as" does, but does not list any downsides of it.
My guess: it probably strikes too many as "excessive novelty", These are assignment expressions. Python's assignment statements put the target at the left. Why change that? ":=" is used for assignment in many more other languages than "->" is. Why fight that?
It conforms to left-to-right evaluation, where name:=expr does not.
? Only "expr" is evaluated, so left-to-right seems irrelevant here. The "evaluation" of a simple name as a binding target is a no-op (no code is generated). If you really do see this as a wart anyway, then it's positively a Good Thing that it's exactly the same "wart" as in Python's assignment statements.
It (I would argue) reduces the asymmetry of the first use of a sub-expression in cases such as [ ( (f(x) -> y)**2, y**3, y**4) for x in iterable ] vs [ ( (y := f(x))**2, y**3, y**4) for x in iterable ] because the first "y" is closer to the way it is used, viz "**2".
The first form reads a little better to me too, but not a lot better. The problem I have with variations of this example on its own (which comes up surprisingly often with minor changes) is that it's clearer spelled today via
[(y**2, y**3, y**4) for y in map(f, iterable)]
Spelling that with either form of assignment expression reads significantly worse than that to my eyes
But more importantly, it's expected that assignment expressions will be used _most_ often to make some common `if` and `while` patterns briefer. Hardly all. Our eyes are already trained to "look at the far right end" for the value being tested, and, e.g.,
while data := sock.recv():
preserves that. Especially in code that doesn't _always_ use assignment expressions in such contexts (which is likely all significant blobs of code), it would be visually jarring to have to "sometimes look in the middle instead" to extract the important part of:
while sockrecv() -> data:
"Look to the left for the name, look to the right for the value" is the rule for assignment statements, assignment expressions, and `for` loop targets.
But there's no "QED" here because this isn't a deductive science. The final answer is "because that's what Guido liked best" ;-)
Thanks, Tim, for a thoughtful answer. Don't get me wrong, I feel quite happy with ":=". Perhaps you have managed to articulate some thoughts that were buried in my subconscious. It's just that I couldn't come up with any rational objections to "->". Rob Cliffe
http://www.avg.com/email-signature?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=emailclient Virus-free. www.avg.com http://www.avg.com/email-signature?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=emailclient