[Python-ideas] "Assignment expression" with function call-alike syntax

Steven D'Aprano steve at pearwood.info
Wed May 23 21:21:04 EDT 2018

On Wed, May 23, 2018 at 12:32:36AM +0300, Kirill Balunov wrote:

> Just one more variation on "assignment exression" syntax to make the list
> more complete :) Sorry, if something similar has already been suggested.
> The idea is to use function's call-like syntax in the from: `this( name =
> expr )`.
> In this way it is somewhat possible to make an assignment in `while` and `if
> ` headers right now, which covers 90% cases, but it is damn slow. Maybe be
> it is worth to make `this` magic call-alike object work fast...on the other
> hand does anyone like `this`?

Apart from needing extra typing, what does the function-call syntax gain
us? It looks like a function you could call from anywhere, but you want 
to limit it to just "while" and "if", I expect that will just give us a 
flood of questions on Stackoverflow and other forums, "why can't I use 
this() outside of if and while loops?"

A good question. Why shouldn't we use assignment outside of if and while?

Since this is special syntax, not a function, the parens don't give us 
any benefit:

    this name = expression

The word "this" seems inappropriate. Surely "let" or "set" would be

    let name = expression

which at least has the benefit of matching syntax chosen in other 
languages. But if the only reason we include "let" is to avoid the 
equals/assignment error, that seems needlessly verbose. We can say the 
same thing more compactly:

    name := expression

The difference between "let name = expression" and "name := expression" 
is just spelling.


More information about the Python-ideas mailing list