[Python-Dev] PEP 572: Assignment Expressions

David Shawley daveshawley at gmail.com
Wed Apr 25 07:03:31 EDT 2018


On Apr 24, 2018, at 2:10 PM, MRAB <python at mrabarnett.plus.com> wrote:
> 
> On 2018-04-21 03:15, Tim Peters wrote:
>> [Tim]
>> >> And I'll take this opportunity to repeat the key point for me:  I
>> >> tried hard, but never found a single case based on staring at real
>> >> code where allowing _fancier_ (than "plain name") targets would be a
>> >> real improvement.  In every case I thought it _might_ help, it turned
>> >> out that it really didn't unless Python _also_ grew an analog to C's
>> >> "comma operator" (take only the last result from a sequence of
>> >> expressions).  I'll also note that I asked if anyone else had a
>> >> real-life example, and got no responses.
>> 
>> [MRAB <python at mrabarnett.plus.com>]
>> > Could a semicolon in a parenthesised expression be an equivalent to C's
>> > "comma operator"?
>> 
>> I expect it could, but I it's been many years since I tried hacking
>> Python's grammar, and I wouldn't want a comma operator anyway ;-)
> [snip]
> Just reading this:
> 
> https://www.bfilipek.com/2018/04/refactoring-with-c17-stdoptional.html
> 
> about C++17, and what did I see? An example with a semicolon in parentheses!

A similar pattern shows up in Go's if statement syntax.  It is interesting to note that it is part of the grammar specifically for the if statement and *not* general expression syntax.

    IfStmt = "if" [ SimpleStmt ";" ] Expression Block [ "else" ( IfStmt | Block ) ] .

Bindings that occur inside of `SimpleStmt` are only available within the `Expression` and blocks that make up the if statement.

https://golang.org/ref/spec#If_statements

This isn't a good reason to parrot the syntax in Python though.  IMO, I consider the pattern to be one of the distinguishing features of golang and would be happy leaving it there.

I have often wondered if adding the venerable for loop syntax from C (and many other languages) would solve some of the needs here though.  The for loop syntax in golang is interesting in that it serves as both a standard multipart for statement as well as a while statement.

Changing something like this is more of a Python 4 feature and I think that I would be -0 on the concept.  I did want to mention the similarities for the posterity though.

ChrisA - we might want to add explicit mentions of golang's if statement and for loop as "considered" syntaxes since they are in a sibling programing language (e.g., similar to async/await in PEP 492).

- dave
--
"Syntactic sugar causes cancer of the semicolon" - Alan Perlis
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20180425/08b07230/attachment.html>


More information about the Python-Dev mailing list