[Python-ideas] Spelling of Assignment Expressions PEP 572 (was post #4)
Chris Angelico
rosuav at gmail.com
Sun Apr 15 16:22:54 EDT 2018
On Mon, Apr 16, 2018 at 12:17 AM, Kirill Balunov
<kirillbalunov at gmail.com> wrote:
>
>
> 2018-04-15 15:21 GMT+03:00 Chris Angelico <rosuav at gmail.com>:
>> I don't think we're ever going to unify everyone on an arbitrary
>> question of "expression first" or "name first". But to all the
>> "expression first" people, a question: what if the target is not just
>> a simple name?
>>
>> while (read_next_item() -> items[i + 1 -> i]) is not None:
>> print("%d/%d..." % (i, len(items)), end="\r")
>>
>
> I completely agree with you that it is impossible to unify everyone opinion
> - we all have different background. But this example is more likely to play
> against this PEP. This is an extra complexity within one line and it can
> fail hard in at least three obvious places :) And I am against this usage no
> matter `name first` or `expression first`. But i will reask this with
> following snippets. What do you choose from this examples:
>
> 0.
>
> while (items[i := i+1] := read_next_item()) is not None:
> print(r'%d/%d' % (i, len(items)), end='\r')
>
> 1.
>
> while (read_next_item() -> items[(i+1) -> i]) is not None:
> print(r'%d/%d' % (i, len(items)), end='\r')
These two are matching what I wrote, and are thus the two forms under
consideration. I notice that you added parentheses to the second one;
is there a clarity problem here and you're unsure whether "i + 1 -> i"
would capture "i + 1" or "1"? If so, that's a downside to the
proposal.
> 2.
>
> while (item := read_next_item()) is not None:
> items[i := (i+1)] = item
> print(r'%d/%d' % (i, len(items)), end='\r')
>
> 3.
>
> while (read_next_item() -> item) is not None:
> items[(i+1) -> i] = item
> print(r'%d/%d' % (i, len(items)), end='\r')
>
> 4.
>
> while (item := read_next_item()) is not None:
> i = i+1
> items[i] = item
> print(r'%d/%d' % (i, len(items)), end='\r')
>
> 5.
>
> while (read_next_item() -> item) is not None:
> i = i+1
> items[i] = item
> print(r'%d/%d' % (i, len(items)), end='\r')
All of these are fundamentally different from what I'm asking: they
are NOT all expressions that can be used in the while header. So it
doesn't answer the question of "expression first" or "target first".
Once the expression gets broken out like this, you're right back to
using "expression -> NAME" or "NAME := expression", and it's the same
sort of simple example that people have been discussing all along.
> I am definitely Ok with both 2 and 3 here. But as it was noted `:=` produces
> additional noise in other places and I am also an `expression first` guy :)
> So I still prefer variant 3 to 2. But to be completely honest, I would write
> it in the following way:
>
> for item in iter(read_next_item, None):
> items.append(item)
> print(r'%d/%d' % (i, len(items)), end='\r')
And that's semantically different in several ways. Not exactly a fair
comparison.
I invite you to write up a better example with a complex target.
ChrisA
More information about the Python-ideas
mailing list