[Python-Dev] Informal educator feedback on PEP 572 (was Re: 2018 Python Language Summit coverage, last part)

Ivan Pozdeev vano at mail.mipt.ru
Wed Jun 27 21:29:46 EDT 2018


On 28.06.2018 2:44, Greg Ewing wrote:
> Ivan Pozdeev via Python-Dev wrote:
>> for me, the primary use case for an assignment expression is to be 
>> able to "catch" a value into a variable in places where I can't put 
>> an assignment statement in, like the infamous `if re.match() is not 
>> None'.
>
> This seems to be one of only about two uses for assignment
> expressions that gets regularly brought up. The other is
> the loop-and-a-half, which is already adequately addressed
> by iterators.
>
> So maybe instead of introducing an out-of-control sledgehammer
> in the form of ":=", we could think about addressing this
> particular case.
>
> Like maybe adding an "as" clause to if-statements:
>
>    if pattern.match(s) as m:
>       do_something_with(m)
>

I've skimmed for the origins of "as" (which I remember seeing maybe even 
before Py3 was a thing) and found this excellent analysis of modern 
languages which is too a part of the PEP 572 discussion:
https://mail.python.org/pipermail/python-ideas/2018-May/050920.html

It basically concludes that most recently-created languages do not have 
assignment expressions; they rather allow assignment statement(s?) 
before the tested expression in block statements (only if/while is 
mentioned. `for' is not applicable because its exit condition in Python 
is always the iterable's exhaustion, there's nothing in it that could be 
used as a variable).

It, however, doesn't say anything about constructs that are not block 
statements but are equivalent to them, like the ternary operator. (In 
comprehensions, filter conditions are the bits equivalent to if/while 
statements.)

-- 
Regards,
Ivan



More information about the Python-Dev mailing list