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

Kirill Balunov kirillbalunov at gmail.com
Sat May 26 05:00:45 EDT 2018

2018-05-24 4:21 GMT+03:00 Steven D'Aprano <steve at pearwood.info>:

> 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?

This will allow us to temporarily avoid the introduction of a new syntax.
Since this option does not require a new syntax, you can provide a
provisional module containing. This will allow to understand more clearly
the actual use cases and the attitude of users towards the general idea.

> 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?"

I do not know how you decided that I propose to limit this call-alike
variant only to `while` and `if`statements. I did not say that. I just said
that personally I'm interested and see the advantages of using it only in
`while` and `if` statements. Of course you are right that you can call it
from anywhere.

> 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
> better:
>     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:

I'm not much attached to `this`. Ok let it be `let`.

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

The main point is to collect more information, since the idea of assignment
expression will have a huge impact in all aspects of Python programming:
how you structure your programm, how you write code, how you read code, how
you parse code... Because at the moment the rule is simple that any name
binding must occur only in statements.

With kind regards,
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180526/02c41726/attachment.html>

More information about the Python-ideas mailing list