[Python-ideas] Inline assignments using "given" clauses

MRAB python at mrabarnett.plus.com
Sat May 12 19:51:56 EDT 2018


On 2018-05-12 22:07, Cameron Simpson wrote:
> On 06May2018 02:00, Nick Coghlan <ncoghlan at gmail.com> wrote:
>>On 5 May 2018 at 13:36, Tim Peters <tim.peters at gmail.com> wrote:
>>> If only one trailing "given" clause can be given per `if` test
>>> expression, presumably I couldn't do that without trickery.
>>
>>I was actually thinking that if we did want to allow multiple assignments,
>>and we limited targets to single names, we could just use a comma as a
>>separator:
>>
>>    if diff and g > 1 given diff = x - x_base, g = gcd(diff, n):
>>        return g
>>
>>Similar to import statements, optional parentheses could be included in the
>>grammar, allowing the name bindings to be split across multiple lines:
>>
>>    if diff and g > 1 given (
>>        diff = x - x_base,
>>        g = gcd(diff, n),
>>    ):
>>        return g
> 
> I'm well behind, but... this! This turns "given" into a +0.8 for me.
> 
 > That's really nice. It reads clearly too.
 >
That's longer than this:

diff = x - x_base
g = gcd(diff, n)
if diff and g > 1:
     return g

which is already valid.

[snip]


More information about the Python-ideas mailing list