[Python-Dev] assignment expressions: an alternative alternative proposal

Steven D'Aprano steve at pearwood.info
Wed Apr 25 01:16:25 EDT 2018


On Tue, Apr 24, 2018 at 03:54:30PM -0700, Guido van Rossum wrote:

> We should really take this back to python-ideas at this point.

Please no :-(

Variants of "let" etc were discussed earlier and didn't seem to get much 
interest. Although I don't think "var" specifically was suggested 
before, "let" certainly has been:

https://mail.python.org/pipermail/python-ideas/2018-February/049031.html

In the same thread, I also independently suggested "let":

https://mail.python.org/pipermail/python-ideas/2018-February/048981.html

This suggestion didn't seem to get much in the way of support, so much 
so that in the hundreds of emails for PEP 572 the idea seems to have 
been completely forgotten until now. Perhaps it ought to be added to the 
PEP in the rejected syntax section.

I don't think there's anything to gain by rehashing this again: whether 
we choose "let", "var" or "bind", there's no real difference. It's still 
a new keyword, and it still looks like little else in Python today:

    while (var spam = expression) and spam in values:
        ...

It's not really awful, but nor is it clearly better than := and the fact 
that it will require a keyword is a major point against it.

I'm speaking for myself, of course, not Chris, but while consensus is of 
course desirable I think there comes a time where the PEP author is 
entitled to say "I've heard enough arguments, and I'm not convinced by 
them, this is my proposal and here it stays". If it were my PEP, at this 
point I would say that if anyone wants to champion an alternative syntax 
they can write their own PEP :-)

I think this idea is going to be too divisive to expect a consensus to 
emerge, rather like the ternary if operator. I truly believe we've 
covered just about everything that needs covering, over many hundreds of 
emails in multiple threads, and unless somebody puts their hand up to 
write a competing PEP (or at least says something new beyond what we've 
already seen many times before) I think it is just about time for a BDFL 
decision between:

1. Reject the PEP. Not going to happen.

2. Leave the PEP pending. Maybe some time in the future.

3. Accept the PEP using the `name := expression` syntax.

My preference would be for 3, even though := isn't my first choice for 
syntax, it's still a good choice. I think there are sufficient use-cases 
to justify this feature, including some long standing annoyances such as 
the dance we have to play with regexes, which only gets more annoying as 
you add more alternatives and cascading indentation.

    mo = re.match(pattern1, string)
    if mo:
        process(mo)
    else:
       mo = re.match(pattern2, string)
       if mo:
            handle(mo)
       else:
           mo = ... # you get the picture


(I've changed my mind from earlier when I said this didn't count as a 
separate use-case from those already given. I think that cascading 
regexes is a big enough pain that it should be explicitly listed in the 
PEP as a motivation.)


-- 
Steve


More information about the Python-Dev mailing list