[Python-ideas] Inline assignments using "given" clauses
Cameron Simpson
cs at cskk.id.au
Sat May 12 20:59:24 EDT 2018
On 13May2018 00:51, MRAB <python at mrabarnett.plus.com> wrote:
>On 2018-05-12 22:07, Cameron Simpson wrote:
>>On 06May2018 02:00, Nick Coghlan <ncoghlan at gmail.com> 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.
>>
>That's longer than this:
>
>diff = x - x_base
>g = gcd(diff, n)
>if diff and g > 1:
> return g
>
>which is already valid.
Yes, but in a slightly larger scope such as a cascaded if/elif/.../else
sequence embedding the "given" into the expression has 2 advantages: it stops
the if-else cascading across the terminal purely to embed assignments before
the next test, and it lets one clearly associate these particular assignments
as relevant to the test itself.
Obviously there are circumstances for each, and I expect overuse of "given"
degrades readability. However, I still find Nick's "if this given that"
formulation quite compelling as a logical form.
Cheers,
Cameron Simpson <cs at cskk.id.au>
More information about the Python-ideas
mailing list