[Python-ideas] Spelling of Assignment Expressions PEP 572 (was post #4)

Kirill Balunov kirillbalunov at gmail.com
Sun Apr 15 10:17:53 EDT 2018


2018-04-15 15:21 GMT+03:00 Chris Angelico <rosuav at gmail.com>:

> On Sun, Apr 15, 2018 at 7:19 PM, Kirill Balunov <kirillbalunov at gmail.com>
> wrote:
> >> === Expression first, 'as' keyword ===
> >>
> >>     while (read_next_item() as value) is not None:
> >>         ...
> >>
> >> Pros:
> >>
> >>   * typically reads nicely as pseudocode
> >>   * "as" is already associated with namebinding operations
> >>
> >
> > I understand that this list is subjective. But as for me it will be huge
> PRO
> > that the expression comes first.
>
> 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")
>
> [...]
>
> Not a rhetorical question. I'm genuinely curious as to whether people
> are expecting "expression -> NAME" or "expression -> TARGET", where
> TARGET can be any valid assignment target.
>
>
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')

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')

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')


With kind regards,
-gdg
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180415/5af66e31/attachment.html>


More information about the Python-ideas mailing list