[Python-ideas] PEP 572: Assignment Expressions (post #4)

Kirill Balunov kirillbalunov at gmail.com
Wed Apr 11 15:24:24 EDT 2018


2018-04-11 18:01 GMT+03:00 Kirill Balunov <kirillbalunov at gmail.com>:

>
>
> 2018-04-11 16:50 GMT+03:00 Chris Angelico <rosuav at gmail.com>:
>
>>
>> Can you give an example of how your syntax is superior to the more
>> general option of simply allowing "as" bindings in any location?
>>
>>
> This is not my syntax :) And not even my idea. I just do not understand,
> and even a little skeptical about allowing "as" bindings in **any
> location** with global scoping. All the examples in this thread and the
> previous ones, as well as almost all PEP's examples show how this feature
> will be useful in `if`, `while` statements and comprehension/generator
> expressions. And it excellently solves this problem.  This feature
> increases the capabilities of these statements and also positively affects
> the readability of the code and it seems to me that everyone understands
> what this means in this context without ambiguity in their meaning in
> `while` or `with` statements.
>
> The remaining examples (general ones) are far-fetched, and I do not have
> much desire to discuss them :)  These include:
>
> lambda: x := lambda y: z := a := 0
> y = ((eggs := spam()), (cheese := eggs.method()), cheese[eggs])
> and others of these kind...
>
> Thus, I do not understand why to solve such a general and complex problem,
> when this syntax is convenient only in specific cases.  In addition,
> previously the concept of a Statement-Local Name Bindings was discussed, which
> I basically like (and it fits the above idea).  In this version, it was
> abandoned completely, but it is unclear for what reasons.
>
> p.s.: Maybe someone has use-cases outside `if`, `while` and
> comprehensions, but so far no one has demonstrated them.
>
>
I find that I wrote very vague, so I'll try in response to my answer to add
some specifics. In general, I find this idea missed in the language and
thank you for trying to fix this! In my opinion it has only a meaning in
certain constructions such as `while`, `if`, `elif` and maybe
comprehensions\generators. As a general form "anywhere" it can be _useful_,
but makes the code unreadable and difficult to perceive while giving not so
much benefit. What I find nice to have:

Extend while statement syntax:

 while (input("> ") as command) != "quit":
     print("You entered:", command)


Extend if statement syntax:



if re.search(pat, text) as match:
    print("Found:", match.group(0))

if (re.search(pat, text) as match) is not None:
    print("Found:", match.group(0))

also `elif` clauses should be extended to support.

Extend comprehensions syntax:

# Since comprehensions have an if clause
[y for x in data if (f(x) as y) is not None]

# Also this form without `if` clause

[(y, x/y) with f(x) as y for x in range(5)]

Extend ternary expression syntax:

data = y/x if (f(x) as y) > 0 else 0

I think that is all. And it seems to me that it covers 99% of all the
use-cases of this feature. In my own world I would like them to make a
local _statement_ binding (but this is certainly a very controversial
point). I even like that this syntax matches the `with` an `except`
statements syntax, although it has a different semantic. But I do not think
that anyone will have problems with perception of this.

With kind regards,
-gdg
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180411/7d600818/attachment-0001.html>


More information about the Python-ideas mailing list