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

Cameron Simpson cs at cskk.id.au
Sat May 12 17:24:20 EDT 2018


On 13May2018 07:07, Cameron Simpson <cs at cskk.id.au> wrote:
>>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.

And if we're still worried about new keywords or reserved words, this:

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

or:

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

or even:

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

read nearly as well to my eye.

My main point here is that "with" works as well as "given" in this form from an 
English prose point of view.

Cheers,
Cameron Simpson <cs at cskk.id.au>


More information about the Python-ideas mailing list